我正在研究ts中的代数数据类型,并坚持输入匹配函数。问题的简化版本如下所示:
let rec = { a: 'a', b: 1 };
// just captures type of 'rec' and then reuses the type for eval
const makeEval = <Record>(rec: Record) => <
Res,
K extends keyof Record = keyof Record
>(
val: Record,
cases:
| Cases<Record, Res>
| (Cases<Record, Res, K> & { else: (r: Record) => Res })
): Res => (undefined as any) as Res;
export type Cases<Record, Res, K extends keyof Record = keyof Record> = {
[T in K]: (value: Record[T]) => Res
};
const evalMyRecord = makeEval(rec);
// this is fine
const a = evalMyRecord(rec, { a: s => s, b: n => n.toString() });
// err
const b = evalMyRecord(rec, { a: s => s, else: _ => 'why err?' });
// Property 'b' is missing in type '{ a: (s: string) => string; else: (_: { a: string; b: number; }) => string; }'.
// requires explicit subset
const b_ = evalMyRecord<string, 'a'>(rec, {
a: s => s,
else: _ => 'but explicit a is fine'
});
// full set is fine
const c = evalMyRecord(rec, {
a: s => s,
b: n => 'n',
else: _ => 'fine too'
});
所以我想表达一个类型,它具有Record的所有键或其任何子集+ {else} case。
我知道Partial有一个解决方案:
type EvalCases<Record, Res> =
| Cases<Record, Res>
| (Partial<Cases<Record, Res>> & { else: (r: Record) => Res });
const b = evalMyRecord(rec, { a: s => s, else: _ => 'works' });
const b2 = evalMyRecord(rec, {
a: s => s,
b: undefined,
else: _ => 'also works but weird'
});
但是:{b:undefined}看起来有点偏。如果任何情况不是一个正确的函数(如果可能的话),我想要编译器错误。
有关打字稿魔术的任何建议吗?
注意:概念上与Typescript cannot infer correct argument types for an object of functions关闭(但不相同)。
答案 0 :(得分:1)
以下解决方案允许您拥有Cases<Record, Res>
或Cases<Record, Res>
的任何子集以及else
属性。
type EnforcingPartial<T> = {
[key in keyof T]: { [subKey in key]: T[key]; }
}[keyof T];
type EvalCases<Record, Res> = Cases<Record, Res> |
(EnforcingPartial<Cases<Record, Res>> & { else: (r: Record) => Res });
const b = evalMyRecord(rec, { a: s => s, else: _ => 'works' });
const b2 = evalMyRecord(rec, {
a: s => s,
b: undefined, // ERROR HERE, b is incompatible
else: _ => 'also works but weird'
});