Definite assignment assertions in Typescript

| Tag typescript  types 

A definite assignment check is TS’s way of making sure that by the time we use a variable, that variable has been assigned a value:

let userId: string;
userId.toUpperCase(); // Variable 'userId' is used before being assigned.

We can use definite assignment assertion to tell TS that the variable will definitely be assigned by the time we read it:

let userId!: string; // notice the ! after variable name
fetchUser();

userId.toUpperCase();

function fetchUser(){
    userId = 'testId';
}

Prev     Next