LoginSignup
1
0

More than 1 year has passed since last update.

[TypeScript] 任意のオブジェクトを表す型定義

Posted at
const obj1: object = {};
obj1.value1 = 'A';
// 動作はするが下記のエラーがVSCodeに表示される
// Property 'value1' does not exist on type 'object'.(2339)

console.log({obj1});

object「型」なのに、代入したら
その時点でのプロパティを持っているかどうかで型が勝手に限定されるらしい。

下記のように書くとよい。

const obj2: {[prop: string]: any} = {};
obj2.value1 = 'B';
1
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
1
0