TypeScript: exactOptionalPropertyTypes
TypeScript handles optional and undefined differently - exactOptionalPropertyTypes will help catch scenarios where undefined
should be typed explicitly.
When turned off:
type User = {
email?: string;
}
const user: User = { email: undefined }
// Valid
When turned on:
type User = {
email?: string;
}
const user: User = { email: undefined }
// Type '{ email: undefined; }' is not assignable to type 'User' with 'exactOptionalPropertyTypes: true'.
// Consider adding 'undefined' to the types of the target's properties.
Read optional vs undefined | TkDodo's blog to learn the difference and how this can help.