typescript
// interface 接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* age?------> ? 属性可有可无
* readname name ----> 属性可读
* [propName: string]: any, -------> 其他多余属性
* 接口可以继承interface
* interface 可以定义一个具体的方法
*/
interface Person {
name: string;
// readonly name: string,
// 可有可无属性
age?: number;
// 可以多出其他属性类型
[propName: string]: any;
say(): string;
}
const getPersonName = (person: Person) => {
console.log(person.name);
console.log(person.age);
};
const setPersonName = (person: Person, name: string) => {
person.name = name;
};
const person = {
name: 'change',
sex: '男',
say() {
return 'say hello ';
},
};
getPersonName(person);
// 直接赋值报错
// getPersonName({
// name: "change",
// sex: '男'
// })
setPersonName(person, 'less');
// 类应用一个接口,必须存在接口里面所有的属性
class User1 implements Person {
name = 'l3';
say() {
return '123';
}
}
// 接口之间互相继承
interface Tea extends Person {
teach(): string;
}
// 接口自身定义函数 ----> 接受一个string类型返回一个string
interface SayHi {
(word: string): string;
}
const say: SayHi = (word) => {
return word;
};
const s:string = say('hello')
console.log(s)