環境情報
TypeScript 3.2.1
※2018/12/12現在 TS Playground で使用されているもの。オプション(noImplicitAny,strictNullChecks,strictFunctionTypes,strictPropertyInitialization,noImplicitThis,noImplicitReturns) はすべて有効。
実現したいこと
組み込み型定義にあるPartialの、キーを指定できるバージョンを作成したいと思っています。
type Foo = { a: string, b: string, c?: string, }; type Bar = MyGenerics<Foo , 'b'>; /** * 以下のような型が作られる * { * a: string, * b?: string, * c?: string, * }; */
試してみたもの(コンパイルエラー)
type MyEntity = { a: string; b?: string; c: string; }; type SelectivePartial<T, K extends keyof T> = { [P in K]?: T[P] } & { [P in Exclude<keyof T, K>]: T[P] }; // type SelectedMyEntity = { c?: string | undefined; } & { a: string; b: string | undefined; } type SelectedMyEntity = SelectivePartial<MyEntity, 'c'>; let x: SelectedMyEntity; /** * COMPILE SUCCESS */ x = { a: '', b: '', c: '' }; /** * COMPILE SUCCESS */ x = { a: '', b: '', // c: '' }; /** * COMPILE ERROR * Property 'b' is missing in type '{ a: string; c: string; }' but required in type '{ a: string; b: string | undefined; }' */ x = { a: '', // b: '', c: '' };

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。