TypeScript標準にDiff型がないけどExcludeという名前で同じ機能
#TypeScript
ここのハンドブックに紹介されているDiff型は、標準にないので自分でコピペして使う感じになる。
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
でもDiffは使うごとにコピペするのはあまり好ましくないので、探してみるとExcludeという名前で存在している。
以下のDiffとExcludeの型を見てわかるように同じ定義。
code:ts
// (from: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html)
type Diff<T, U> = T extends U ? never : T; // Remove types from T that are assignable to U
code:ts
/**
* Exclude from T those types that are assignable to U
*/
type Exclude<T, U> = T extends U ? never : T;
知ったきっかけ
TypeScriptで特定のプロパティがない型を定義したい