How to declare nullable type in TypeScript ? Last Updated : 15 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In vanilla JavaScript, there are two primary data types: null and undefined. While TypeScript previously did not allow explicit naming of these types, you can now use them regardless of the type-checking mode. To assign undefined to any property, you need to turn off the --strictNullChecks flag. However, with this flag enabled, you cannot assign undefined to members that do not have the nullable operator. This update in TypeScript enhances type safety, allowing developers to explicitly handle cases where values may be absent, ultimately reducing runtime errors and improving code quality.Example: The below example represents what happens if the --strictNullChecks flag is enabled and the following code is executed. javascript interface Student { name:string; age:number; } let s: Student = { name:'Ajay', age:null } 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. javascript interface Student { name:string; age?:number; } let s: Student = { name:'Ajay', age:null } Comment More infoAdvertise with us Next Article How to express a Date Type in TypeScript ? T techni621 Follow Improve Article Tags : TypeScript JavaScript-Misc Similar Reads How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in 2 min read How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar 3 min read How to express a Date Type in TypeScript ? In TypeScript, the Date object is used to handle dates and times. To express a Date type, declare a variable with the type annotation Date and assign it a date value. This allows for precise date manipulation and formatting in your TypeScript code.Ways to Express Date Type in TypeScriptTable of Cont 3 min read How to Create Deep Readonly Type in Typescript? In TypeScript, the readonly access modifier is a powerful tool that ensures immutability by marking properties of a class as immutable. Once a property is markedreadonly, it cannot be reassigned. This is highly useful for maintaining consistent and safe data structures, especially in scenarios such 3 min read How to Exclude Property from Type in TypeScript ? In Typescript, sometimes we need to exclude a property from a type when we want a similar type with some properties excluded or if we want to remove a property from the type. There are several approaches to exclude properties from a type in typescript: Table of Content Using Mapped Types with condit 4 min read How to use Type Guards in TypeScript ? Here are the methods to use type guards in TypeScript:1. Using typeof Type GuardsThe typeof operator checks the type of a variable, primarily for primitive types like string, number, boolean, etc. JavaScriptfunction processValue(value: string | number) { if (typeof value === 'string') { console.log( 3 min read Like