5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OSSのTypeScriptのソースでよくみかけるDictについて

Last updated at Posted at 2022-01-08

Dict型

export type Dict<T = any> = Record<string, T>;

OSSのソースなどを読んでるとこんな感じでDict型を指定してる事があります

Dict型の目的

let obj={}
obj.name="Alex" //Property 'name' does not exist on type '{}'

これはjavascriptだとエラー起きないのですが、typescriptだとエラーが起きるので、

let obj:{[key:string]:string}={}
obj.name="Alex"

にする必要があります。ここでRecordを使うとより簡潔に

let obj:Record<string,string>={}
obj.name="Alex"

とする事ができます。それを更に簡潔にしたのがDict型という感じですね。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?