RecipesChapter 3: The Type System

Distinguishing Missing Properties and Undefined Values

Recipe 3.14 from The TypeScript Cookbook

// exactOptionalPropertyTypes is true
const settingsUndefinedTheme: Settings = {
  language: "de",
  theme: undefined,
};

// Error: Type '{ language: "de"; theme: undefined; }' is 
// not assignable to type 'Settings' with 'exactOptionalPropertyTypes: true'. 
// Consider adding 'undefined' to the types of the target's properties.
// Types of property 'theme' are incompatible. 
// Type 'undefined' is not assignable to type
// '"dracula" | "monokai" | "github"'.(2375)
Open in TypeScript Playground →