LoginSignup
10
6

More than 3 years have passed since last update.

【Vue】 TypeScript 「Property ‘XXX’ has no initializer and is not definitely assigned in the constructor.」の対処法

Last updated at Posted at 2020-11-05

エラー内容

Property ‘XXX’ has no initializer and is not definitely assigned in the constructor.

piyoプロパティが初期化されていないよ。コンストラクターで明確に割り当てられていないよ。
とのこと。

解消法

hoge.vue
@Component
export default class hoge extends Vue {
  @Prop()
  private piyo: hogehoge[];

こうなっているのを

hoge.vue
@Component
export default class hoge extends Vue {
  @Prop()
  private piyo!: hogehoge[];

こうする。
piyoプロパティに型アサーション「!」を付けることで解決する。

原因

親コンポーネントで値が割り当てられた事を、明示的に宣言していないから。
TypeScriptは静的に割当済みかどうか判断することができないため、エラーを返してしまう。
感嘆符(!)を付けることで、TypeScriptに値が確実に割り当てられいる事を伝えられる。

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6