我刚开始学打字,所以我有一个关于语法的问题.
在对象类型的类型别名中,属性是用分号(;)还是逗号(,)分隔的?
我从The TypeScript Handbook看,;
是用的.
// object literal type
type Person = {
name: string;
age: number;
};
然而,在Web上的其他代码示例中,例如w3schools,我看到逗号(,).
type CarYear = number
type CarType = string
type CarModel = string
type Car = {
year: CarYear,
type: CarType,
model: CarModel
}
我认为这段带有逗号的代码是错误的,但它似乎可以很好地转换为JavaScript.
顺便说一句,在这些看起来相似但实际上是different个例子中,逗号是正确的.
// inferred type from setting to an object literal
const car = {
year: 2009,
type: "Toyota"
};
// defining an object while using a type alias.
const car: Car = {
year: 2009,
type: "Toyota"
};
// tuple type
type Data = [
location: Location,
timestamp: string
];
但回到主要问题上来: 为了分隔对象类型的类型别名中的属性, 逗号(,)是不是错了?或者也可以接受?为什么?