kolypto
kolypto

Reputation: 35483

TypeScript: exclude "null" and "undefined" from a value of type

Support there is some value:

let value: string | null;

then somewhere along the way I'm certain that it's never null. But there is a function that expects a non-nullable type:

function my_func(arg: string){}

How do I tell TypeScript that my value is alright for it?

my_func(value);
// Argument of type 'string | null' is not assignable 
// to parameter of type 'string'

Upvotes: 0

Views: 303

Answers (1)

kolypto
kolypto

Reputation: 35483

Simply use !:

my_func(value!);

Upvotes: 1

Related Questions