网站搜索

typescript - 允许将 null 和 undefined 作为参数的简写方式


有没有一种简写方法告诉 TypeScript 一个参数可以是 null 和 undefined,甚至可能允许任何 falsy 值?

我可以做到这一点,但它不接受 null 作为参数

function a(b?: string | number) {}

我可以做到这一点,但当你有多个参数时,函数定义的长度会开始变长。

function a(b?: string | number | null) {}