TypeScript:从类型值中排除"null"和"undefined"



支持有一些值:

let value: string | null;

那么在某个地方,我确定它永远不会为空。但是有一个函数需要一个非空类型:

function my_func(arg: string){}

我如何告诉TypeScript我的值对它是合适的?

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

直接使用!:

my_func(value!);

相关内容

最新更新