To assign “undefined” to any property, the –strictNullChecks flag has to be turned off. Keeping this flag on won’t allow to assign “undefined” to members that has no nullable operator.
The below example represents what happens if the –strictNullChecks flag is enabled and the following code is executed.
It will throw the below error:
Type 'null' is not assignable to type 'number'
This error won’t appear if we have –strictNullChecks flag turned off. The strictNullChecks flag protects from referencing nulls or undefined values in the code. It can be enabled by adding the -–strictNullChecks flag as an option to the command-line compiler or adding it to the tsconfig.json file.
There is a difference between the Nullable type and “optional”. In “optional”, we provide some default value to the member or let it not be considered as a member. However if any member is assigned optional and then still assigned a value as “null” or “undefined”, then it will throw an error.
- What is the difference between interface and type in TypeScript ?
- How to declare a global variable in PHP?
- declare command in Linux with Examples
- How to declare and access pointer variable in Golang?
- Hello World in TypeScript
- TypeScript class
- Variables in TypeScript
- TypeScript Accessor
- TypeScript Numbers
- TypeScript String
- TypeScript Union
- TypeScript Tuples
- TypeScript Arrays
- Classes in TypeScript
- Data types in TypeScript
- What are the differences between any vs Object in TypeScript?
- TypeScript | toString() Function
- TypeScript | Array map() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.