LoginSignup
0
0

More than 1 year has passed since last update.

TypeScript で、どんなプロパティもセットできる空オブジェクトを作る

Last updated at Posted at 2022-11-12

空オブジェクトを作って、何かしらプロパティをセットしようとすると怒られる。

const obj = {};
obj.foo = 1;  // Property 'foo' does not exist on type '{}'.

当然であるobj は {} 型、つまりいかなるプロパティも持ってはいけない型であるため。

どんなプロパティもセットできる空オブジェクトを作りたい場合は下記でできる。

const obj = Object.create({});

実質やっていることは

const obj: any = {};

と同じなのだが、こちらは typescript-eslint の no-explicit-any というルールに引っかかる。

どうしても上記の方法でいきたい場合は、ルールを ignore する。

/* eslint @typescript-eslint/no-explicit-any: 0 */
const obj: any = {};
0
0
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
0
0