中級 › 文字列リテラルをenum型の引数に渡すとエラーになる理由 › パターン2
文字列リテラルをenum型の引数に渡すとエラーになる理由
enumで定義した型は、見た目が近い文字列リテラルであっても別の型として扱われます。
1 enum Color { 2 Red, 3 Blue, 4 } 5 function paint(c: Color): string { 6 return `color=${c}`; 7 } 8 console.log(paint(_________)); ^
main.ts(8,19): error TS2345: Argument of type '"Red"' is not assignable to parameter of type 'Color'.
空欄を埋めてコンパイルを通す
文字列の"Red"とColor.Redは別の型です。enumのメンバーそのものを渡す必要があります。
次の問題