Nullable types in TypeScript
In recent TypeScript versions, null
and undefined
are not readable. However, newer versions support this.
This tutorial will discuss the concept of nullable types in TypeScript.
Nullable types in TypeScript
null
Users must turn off type checking mode to use the and types
in TypeScript undefined
. Use strictNullChecks
the flag to check the type of a property.
If strictNullChecks
the flag is on, user-defined types null
and will not be allowed undefined
. By default, the flag is on and the user must turn it off manually.
Code:
interface Info {
name: string,
age: number,
city: string,
}
const info: Info = {
name: 'John',
age: null,
city: 'Washington'
}
Since the type checker flag is turned on, there will be no output. An error will be raised, ie 'Type null is not assignable to type number'
.
If the user manually turns off the type checker flag, the code will execute.
Code:
interface Info {
name: string,
age: number,
city: string,
}
const info: Info = {
name: 'John',
age: null,
city: 'Washington'
}
console.log(info)
Output:
{
"name": "John",
"age": null,
"city": "Washington"
}
Now the above code will only be executed when the type checking flag is off.
Making properties optional in TypeScript
There is another way to eliminate null
and undefined
. This is the most preferred way to handle attributes.
TypeScript allows you to make properties optional so that they can be used only when needed. Rather than declaring a property every time it is not used null
, it is better to prefer optional presentation.
Code:
interface Info {
name: string,
age?: number,
city: string,
}
const info1: Info = {name: 'John', city: 'Washington'};
const info2: Info = {name: 'Jack', age: 13, city: 'London'};
console.log(info1);
console.log(info2);
Output:
{
"name": "John",
"city": "Washington"
}
{
"name": "Jack",
"age": 13,
"city": "London"
}
请注意
, in the above example, the attribute age is optional. The attribute age is not required in the first object info1, so it is never called.
If the type checker is off and the user info1
sets the age in to null
, it will throw an error. Now, suppose there are 100 objects and only 10 of them require age
the property, so instead of age
declaring as null
, make it optional.
Nullable Union Types in TypeScript
null
Union types are used when the user does not want to turn off the type checker. Union types are also the preferred approach, but they have the complexity of
being allocated when using properties .
Code:
interface Info {
name: string,
age: number | null,
city: string,
}
const info1: Info = {name: 'John', age: null, city: 'Washington'};
const info2: Info = {name: 'Jack', age: 13, city: 'London'};
console.log(info1);
console.log(info2);
Output:
{
"name": "John",
"age": null,
"city": "Washington"
}
{
"name": "Jack",
"age": 13,
"city": "London"
}
Note age
the declaration in the interface. Union types are used to assign multiple types to properties and work properly even in strict mode.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Handling exceptions with try..catch..finally in TypeScript
Publish Date:2025/04/15 Views:170 Category:TypeScript
-
This article will discuss try..catch..finally handling exceptions in TypeScript using the statement. Handling exceptions in TypeScript In TypeScript, try..catch..finally a block handles exceptions that occur during program runtime. It allow
Convert JSON object to a class in TypeScript
Publish Date:2025/04/15 Views:80 Category:TypeScript
-
Data on the internet flows in the form of strings, which can be converted into a very popular format called JSON. The JSON representation of this data usually represents an object or even a class in TypeScript. TypeScript provides the abili
Update TypeScript to the latest version using NPM
Publish Date:2025/04/15 Views:119 Category:TypeScript
-
This article provides a guide to update to the latest version of TypeScript using npm, which is the best and most popular method used by developers. This also gives us an in-depth look at what npm is and why to use it. Node Package Manager
Using jQuery and TypeScript
Publish Date:2025/04/15 Views:148 Category:TypeScript
-
This article provides the basic understanding and concepts of using jQuery with TypeScript. It guides on how to use jQuery in TypeScript through a coding example and outputs using various methods in TypeScript and provides knowledge about w
Checking for undefined in TypeScript
Publish Date:2025/04/15 Views:196 Category:TypeScript
-
This article will demonstrate how programmers can check for undefined in TypeScript using various coding examples and situations. It not only gives you an understanding of checking for undefined in TypeScript, but also helps in differentiat
exclude attribute in TypeScript
Publish Date:2025/04/15 Views:190 Category:TypeScript
-
This article will discuss excluding properties from a TypeScript type. The exclude property is used to create another type from an existing type with fewer or modified properties. Omit Exclude properties from types in TypeScript TypeScript
Function return types in TypeScript
Publish Date:2025/04/15 Views:174 Category:TypeScript
-
TypeScript is a strongly typed language and it is always encouraged to define the types correctly for all variables used in TypeScript. These types can help in development later, especially during debugging. In addition to variables, functi
Dictionary or map type in TypeScript
Publish Date:2025/04/15 Views:131 Category:TypeScript
-
Dictionaries or maps are used to quickly retrieve items from an object. TypeScript does not have any concept of maps or dictionaries. Plain JavaScript has objects that can set and retrieve key-value pairs. TypeScript provides Record types t
Regular expressions in TypeScript
Publish Date:2025/04/15 Views:104 Category:TypeScript
-
RegExp Is a regular expression used to match a fixed set of characters in a target string using TypeScript. RegExp Objects have many properties and methods. Each property has different options when pattern matching. Using regular expression