初級 › is not assignable to type の原因と直し方 › パターン3
is not assignable to type の原因と直し方
変数への代入・関数の戻り値・プロパティへの代入など、宣言した型と違う型の値を渡そうとしたときにtscが検出するエラーです。
1 interface Item { 2 price: number; 3 } 4 const item: Item = { price: ______ }; ^ 5 console.log(item.price);
main.ts(4,22): error TS2322: Type 'string' is not assignable to type 'number'.
空欄を埋めてコンパイルを通す
Item.priceはnumber型なので、文字列の"9.99"は代入できません。数値の9.99を渡す必要があります。
次の問題