我想检查一个类型是否可为空,以及它的值是否具有条件类型。
我尝试实施
type IsNullable<T> = T extends null ? true : false;
但是,它似乎不起作用
type test = IsNullable<number> // Returns false as it should
type test = IsNullable<number | null> // Returns false when it should be true
检查类型是否可为空的正确方法是什么?我尝试过T extends null | T
,也没有用。
您可以切换的左侧和右侧extends
,因此
type IsNullable<T> = null extends T ? true : false;
应该为您工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句