-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ts-exact-optional-property-types.md
- Loading branch information
1 parent
e2f670a
commit bf9ff68
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# TypeScript: exactOptionalPropertyTypes | ||
|
||
TypeScript handles optional and undefined differently - [exactOptionalPropertyTypes](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) will help catch scenarios where `undefined` should be typed explicitly. | ||
|
||
When turned off: | ||
|
||
```ts | ||
type User = { | ||
email?: string; | ||
} | ||
|
||
const user: User = { email: undefined } | ||
// Valid | ||
``` | ||
|
||
When turned on: | ||
|
||
```ts | ||
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](https://tkdodo.eu/blog/optional-vs-undefined) to learn the difference and how this can help. |