You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
570 B
32 lines
570 B
// 使用接口来描述对象的类型,达到复用的目的
|
|
|
|
// 接口:只能为对象指定类型
|
|
interface IPerson {
|
|
name: string
|
|
age: number
|
|
sayHi(): void
|
|
}
|
|
|
|
// 对象使用接口
|
|
let Iperson: IPerson = {
|
|
name: 'Ken',
|
|
age: 20,
|
|
sayHi() {}
|
|
}
|
|
|
|
|
|
// type(类型别名):可以为对象指定类型,还可以任意类型指定别名
|
|
type IPerson2 = {
|
|
name: string
|
|
age: number
|
|
sayHi(): void
|
|
}
|
|
|
|
let Iperson2: IPerson2 ={
|
|
name: 'Ken',
|
|
age: 20,
|
|
sayHi() {}
|
|
}
|
|
|
|
// 任意类型指定别名
|
|
type NumStr = number | string
|