Type definition in object literal in TypeScript

后端 未结 9 2527
粉色の甜心
粉色の甜心 2020-11-27 09:29

In TypeScript classes it\'s possible to declare types for properties, for example:

class className {
  property: string;
};

How do declare

9条回答
  •  死守一世寂寞
    2020-11-27 09:53

    If you're trying to write a type annotation, the syntax is:

    var x: { property: string; } = ...;
    

    If you're trying to write an object literal, the syntax is:

    var x = { property: 'hello' };
    

    Your code is trying to use a type name in a value position.

提交回复
热议问题