Test if an object has an optional property.
The object type
The property key to check
Returns true only for properties declared with ?. Does not consider union types that include undefined.
true
?
type Obj = { a: string; b?: number; c: number | undefined };type IsOpt_b = HasOptionalProperty< Obj, 'b' >; // truetype IsOpt_c = HasOptionalProperty< Obj, 'c' >; // false Copy
type Obj = { a: string; b?: number; c: number | undefined };type IsOpt_b = HasOptionalProperty< Obj, 'b' >; // truetype IsOpt_c = HasOptionalProperty< Obj, 'c' >; // false
Test if an object has an optional property.