LoginSignup
5

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型という感じですね。

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