【发布时间】:2021-12-05 07:12:42
【问题描述】:
我正在使用打字稿创建一个用于训练目的的计算系统,但在除法期间出现打字错误。
你知道怎么解决吗?
type Variable = {
value: number
resolve: () => number
}
type NoZeroVariable = {
value: Omit<number, 0>
resolve: () => Omit<number, 0>
}
// then when I try to resolve the operation
a.resolve() / b.resolve()
我收到此错误:
The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2363)
【问题讨论】:
-
我认为您遗漏了算术代码。
a和b在 your example 中是undefined,并且没有执行任何算术的代码。 -
Omit<number, 0>不会将0从所有可能的数字中排除。请查看Omit文档typescriptlang.org/docs/handbook/… -
我想你想用
Exclude而不是Omit -
TypeScript 中没有否定类型;您可以过滤工会,但
number不是工会。 TypeScript 中没有与非零数字完全对应的特定类型。你可以像 Captainyossarian 那样写一个通用的约束,但这意味着所有触及它的东西也需要是通用的。
标签: typescript types type-inference divide-by-zero