TypeScript Reference ยท Literal and Object Types
TypeScript optional properties (the ? modifier)
Reference for optional properties in TypeScript: the ? modifier, how it adds undefined, defaults with ??, exactOptionalPropertyTypes and the Partial utility.
Putting a question mark after a property name marks it optional: the object may leave the property out. Reading an optional property yields the declared type plus undefined, so strict mode forces you to handle the missing case before you use the value.
Optional properties are how you describe options objects, partially filled forms and API responses with fields that may or may not be present. The same ? works on method names, on tuple elements and, with a different meaning, on function parameters.
interface Settings { theme: string; fontSize?: number; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
interface T { a?: string; // property m?(): void; // method } type Tuple = [number, string?]; function f(x?: number) {} // parameter type AllOptional = Partial<T>; type AllRequired = Required<T>;
| Form | Description |
|---|---|
name?: T |
The property may be missing; its type is T | undefined when read. |
-? in a mapped type |
Removes optionality, as Required<T> does. |
+? or ? in a mapped type |
Adds optionality, as Partial<T> does. |
obj?.name |
Optional chaining: safe access on a possibly missing object. |
Edge cases: an optional property can be assigned undefined explicitly unless exactOptionalPropertyTypes is on. With that option, { fontSize: undefined } is an error for fontSize?: number, and you must write fontSize?: number | undefined to allow it.
Usage notes
- The second example fails on purpose:
b.labelis possiblyundefined(error 18048), and the compiler asks you to check first. - Prefer
??for defaults.||also replaces0and"", which are often valid values. "fontSize" in objandobj.fontSize !== undefineddiffer for properties that exist with anundefinedvalue, as the last line of the first example shows.- An optional property is not the same as a required property of type
T | undefined: the latter must be written out in every object literal. - Optional properties in a target type accept source objects that lack them, but a source with an optional property is not assignable to a target that requires it.
- Destructuring gives defaults:
const { fontSize = 14 } = settings. - Excess property checks still apply, so typos in option names are caught even when every property is optional. A type with all-optional properties is called a weak type, and assigning something that shares no property with it is an error.
Partial<T>is the quickest way to make every property optional for update functions.