AppendToObject<T, K, V>
使用例
tstype Test = { id: '1' };
type Result = AppendToObject<Test, 'value', 4>; // expected to be { id: '1', value: 4 }
定義例
Mapped Typesを使って1つのRecordとして定義する
1.tstype AppendToObject<
T extends Record<string, unknown>,
K extends string,
V
> = {
[P in K | keyof T]: P extends keyof T ? T[P] : V;
};
条件分岐のある最も無難な定義という感じ
2.tstype AppendToObject<
T extends Record<string, unknown>,
U extends string,
V,
> = MergeIntersections< T & { [K in U]: V; } >;
最も直観的
3.tstype AppendToObject<
T extends Record<string, unknown>,
K extends string,
V,
R = T & { [P in K]: V },
> = {
[P in keyof R]: R[P];
};