2017-10-05 17 views
1

Xのタイプのプロパティのみを選択するTypeScriptタイプを作成することはできますか?TypeScript:定義済みのタイプのプロパティを選択する

interface IA { 
    a: string; 
    b: number; 
} 

interface IB extends IA { 
    c: number | string; 
} 

type IAStrings = PickByType<IA, string>; 
// IAStrings = { a: string; } 
type IBStrings = PickByType<IB, string>; 
// IBStrings = { a: string; } 
type IBStringsAndNumbers = PickByType<IB, string | number>; 
// IBStringsAndNumbers = { a: string; b: number; c: number | string; } 

答えて

0

enhancements to Mapped Typesを使用して今後これを行うことができます。

interface IA { 
    a: string; 
} 

interface IA2 extends IA { 
    b: number; 
} 

interface IB extends IA2 { 
    c: number | string; 
} 

これはあなたに計算しようとしていたあなたのIAタイプを与える:それは理にかなっている場合、私はこれだけをお勧めしますが、

それまでは、あなたは、あなたのインターフェイスを分解することができます。

これが意味をなさない場合は、手動でIAStringsインターフェイスを作成する場合があります。これには、必要なプロパティだけが含まれています。

関連する問題