【发布时间】:2022-01-10 07:34:36
【问题描述】:
有没有一种类型安全的方式来表达这个函数?在此示例中,我添加了数字,但问题是关于一般模式。
type signature =
((a: number, b: number) => number) &
((a: bigint, b: bigint) => bigint);
// Operator '+' cannot be applied to types 'number | bigint' and 'number | bigint'.
const add : signature = (a, b) => a + b;
这两个参数可以是两种不同的类型,但绝不能同时是两种。有没有办法编写这个函数类型让它工作?
【问题讨论】:
-
相关,可能足够接近骗子-stackoverflow.com/q/59745527我还没有找到好方法
-
这很不幸。这可能会有所不同,因为不涉及类型保护。
-
见this
标签: typescript types