中級 › インデックスシグネチャと矛盾する型のプロパティを宣言するとエラーになる › パターン3
インデックスシグネチャと矛盾する型のプロパティを宣言するとエラーになる
[key: string]: numberのようなインデックスシグネチャを持つinterfaceでは、個別に宣言する具体的なプロパティも、インデックスシグネチャの型と互換性がなければ宣言できません。
1 interface Flags { 2 [key: string]: boolean; 3 _______________; ^ 4 } 5 console.log("ok");
main.ts(3,3): error TS2411: Property 'count' of type 'number' is not assignable to 'string' index type 'boolean'.
空欄を埋めてコンパイルを通す
このインデックスシグネチャはboolean型を要求します。countをnumber型で宣言すると矛盾します。
次の問題