What is “type '{}'”?

前端 未结 3 1361
我寻月下人不归
我寻月下人不归 2020-12-11 14:45

In TypeScript, what exactly is type \'{}\' and how does it relate to other built-in types?

For example, the last line of the following example gives

3条回答
  •  抹茶落季
    2020-12-11 15:19

    As far as I know, {} casts directly to a hash map like usage. You can only use keyed properties, versus the dot notation.

    var o:{} = {}
    

    should be equivalent to

    var o:{[key:string]:any} = {}
    

    So

    o.x = 5; //fails
    

    But

    o['x'] = 5; //succeeds
    

提交回复
热议问题