TypeScript の「not assignmentable to param of type never」エラーとは何ですか? 質問する

TypeScript の「not assignmentable to param of type never」エラーとは何ですか? 質問する

コードは次のとおりです:

const foo = (foo: string) => {
  const result = []
  result.push(foo)
}

次の TS エラーが発生します:

[ts] Argument of type 'string' is not assignable to parameter of type 'never'.

何が間違っているのでしょうか? これはバグでしょうか?

ベストアンサー1

result次のように、文字列配列として定義するだけです。

const result : string[] = [];

配列の型を定義しないと、デフォルトで になりますnever。そのため、文字列を追加しようとすると、型の不一致が発生し、表示されたエラーが発生しました。

おすすめ記事