LoginSignup
21
12

More than 3 years have passed since last update.

JavaScriptからTypeScriptへ移行した際に Property does not exist on type '{}'. で怒られた話

Posted at

どうも、最近TypeScriptを書いてます(仮) entakuです。

今回はJavaScriptからTypeScriptへ何も考えずに移行した際に 怒られた話です。

1.発生事象

何も考えずに移行した後、いつものように npm run dev すると以下のように怒られました。

 ERROR  ERROR in /Users/endo/{プロジェクト名}/{フォルダ名}/{ファイル名}.ts                                                                      09:38:19
89:32 Property {パラメータ名} does not exist on type '{}'.

ページは見えるのですが気持ち悪いですね。
これでも動くは動くんですよ。

2.原因調査

恒例のstackoverflow先輩!!!
https://stackoverflow.com/questions/34274487/property-does-not-exists-on-type

let bar = <any>{};
bar.foo = "foobar"; 

なるほど型を指定しないとダメなようです。
{パラメータ名}はクラス定義したものから参照していたのですが、そのためエラーが出ていました。
今回はanyにしないとダメですね。

3.対応

stackoverflow先輩の言うとおりにします。

let {パラメータ名} = <any>{};

image.png

🎉🎉🎉🎉🎉🎉

4.まとめ

早く型安全な世界にしたい

21
12
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
21
12