likes
comments
collection
share

[TS] interface, type, class 中的分号,逗号

作者站长头像
站长
· 阅读数 26
// ↓ 接口 interface
interface Interface1 {
    name:string; // ← 分号
    age:number, // ← 逗号
    sex?:string // ← 不写分号,也不写逗号
    interests?:string[];
}
// ↓ 类型 type
type Type1 = {
    name:string;
    age:number,
    sex?:string
    interests?:string[];
}
const in1:Interface1 = {name:'张三', age:18}
console.log(in1.name);

const t1:Type1 = {name:'李四', age:13}
console.log(t1.age);

上面关于 interface, type的写法都没有语法错误(不会报错)

结论:interface 和 type 中可以用分号或逗号,注意:class 中只能用分号。

转载自:https://segmentfault.com/a/1190000042057671
评论
请登录