我很难确定<代码>Result的类型,例如:
<代码>Result应为以下模式之一:
type Result<Some> = { code: 0; some_result: ...}
// or
type Result = { code: NonZero; message: string}
如何分类?
或许这个问题可以分为:
- implement
NonZero
(p.s.Exclude<number, 0>
does not work) - give typing to the generic
Result
Updates
我写了以下工作纲要:
type Result<TCode extends number, T extends {} = {}> = TCode extends 0
? { code: 0; } & T
: { code: TCode; reason: string };
type SomeResult = {some_result: string}
function getSomeResult(): Result<0 | -1 | 6007 | 114514, SomeResult> {
return {
code: 0,
some_result:
}
}
const result = getSomeResult()
switch (result.code) {
case 0:
// this is wrong: reason should be void
result.reason
// this is ok:
result.some_result
break
default:
// this is ok: there s always a reason when code is non-zero
result.reason
}
现在,STS汇编商可以推断其他财产无效,但我需要把所有可能的非零件印成“代码<>/代码>的类型参数。
如何分类<代码>NonZero