关于前端:TS-interface-type-class-中的分号逗号

// ↓ 接口 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 中只能用分号。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理