-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts-2.ts
More file actions
135 lines (105 loc) · 2.44 KB
/
ts-2.ts
File metadata and controls
135 lines (105 loc) · 2.44 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//接口:接口能够描述JavaScript中对象,函数拥有的各种各样的外形。
//一个简单的伪接口,描述一下接口是如何工作的
function sayName(animal: {name: string}) {
console.log(animal.name);
}
var cat = {name: 'tom'};
sayName(cat);
//第一个接口
interface Animal {
name: string;
}
function sayName1(animal: Animal) {
console.log(animal.name);
}
let cat1 = {name: 'tom'};
sayName1(cat1);
//接口中的属性不是必须的
interface People{
name: string;
couple?: string;//可选属性,可以对接口进行预定义,还可以避免属性检查的错误
}
function getPeople(person: People): {name: string; couple: string} {
let liuyi = {name: 'liuyi', couple:'nana'};
if(person.name){
liuyi.name = person.name;
}
if(person.couple){
liuyi.couple = person.couple;
}
return liuyi;
}
let lala = getPeople({name:'lalala'})
//只读属性,如果你通过接口创建了一个对象字面量,其属性不能再被修改
interface Point{
readonly x: number;
readonly y: number;
}
let point = {1,2}
point.x = 3;//error
//ReadonlyArray<T>只读数组类型
let a = [1, 2, 3];
let a1: ReadonlyArray<number> = a;
a1 = 2;//报错
var s: [] = a1;//报错,就算把只读数组赋值给一个普通数组也是不可以的
//但是可以使用数组断言才能进行赋值
var s1 = [];
s1 = a1 as number[];
//函数类型
interface SearchFunc{
(source: string, substring: string):number;
}
let mySearch: SearchFunc;
mySearch = function(source: string,substring: string):number {
let result = source.search(substring);
return result;
}
//接口继承
interface Shape {
color: string;
}
interface Square extends Shape {
sideLength: number;
}
let square = <Square>{};
//多继承
interface Shape {
color: string;
}
interface PenStroke {
penWidth: number;
}
interface Square extends Shape, PenStroke {
sideLength: number;
}
let square1 = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
//类
class Greeter {
greeting: string;
//类的构造器
constructor(message: string){
this.greeting = message;
}
greet(){
return "Hello,"+this.greeting;
}
}
let greeter = new Greeter("world");
//继承类
class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
}
const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();