Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1fc427

Browse files
author
victorsun
committed
ts
1 parent c8ab31f commit d1fc427

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

‎18-TypeScript/04-oop/03-interface.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function() {
22

33
type myType = {
4-
name: string,
4+
readonlyname: string,
55
age: number,
66
[propName: string]: any,
77
}
@@ -27,6 +27,19 @@
2727
gender: 'male',
2828
}
2929

30+
31+
32+
// 函数类型接口
33+
interface SearchFunc {
34+
(source: string, subString: string): boolean;
35+
}
36+
let mySearch: SearchFunc;
37+
mySearch = function(source: string, subString: string) {
38+
let result = source.search(subString);
39+
return result > -1;
40+
}
41+
42+
3043
// interface 和 abstract 不同的是 abstract 可以有具体的方法,interface 所有的方法都是抽象方法
3144
// 接口定义了类的规范标准
3245
interface myInter {
@@ -44,5 +57,45 @@
4457
}
4558
}
4659

60+
// 类类型
61+
interface ClockConstructor {
62+
new (hour: number, minute: number): ClockInterface;
63+
}
64+
interface ClockInterface {
65+
tick(): void;
66+
}
67+
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
68+
return new ctor(hour, minute);
69+
}
70+
class DigitalClock implements ClockInterface {
71+
constructor(h: number, m: number) { }
72+
tick() {
73+
console.log("beep beep");
74+
}
75+
}
76+
class AnalogClock implements ClockInterface {
77+
constructor(h: number, m: number) { }
78+
tick() {
79+
console.log("tick tock");
80+
}
81+
}
82+
// 传入类构造函数
83+
let digital = createClock(DigitalClock, 12, 17);
84+
let analog = createClock(AnalogClock, 7, 32);
85+
86+
87+
// 接口继承
88+
interface Shape {
89+
color: string;
90+
}
91+
interface PenStroke {
92+
penWidth: number;
93+
}
94+
interface Square extends Shape, PenStroke {
95+
sideLength: number;
96+
}
97+
let square = <Square>{};
98+
99+
47100

48101
})();

‎18-TypeScript/04-oop/05-泛型.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ class MyClass<T> {
3434
this.name = name;
3535
}
3636
}
37-
const mc = new MyClass<string>('csxiaoyao');
37+
const mc = new MyClass<string>('csxiaoyao');
38+
39+
40+
// 使用带有调用签名的对象字面量来定义泛型函数
41+
function identity<T>(arg: T): T {
42+
return arg;
43+
}
44+
let myIdentity: {
45+
<T>(arg: T): T
46+
} = identity;
47+
interface GenericIdentityFn<T> {
48+
(arg: T): T;
49+
}
50+
let myIdentity2: GenericIdentityFn<number> = identity;
51+
52+
// 在泛型里使用类类型 在TypeScript使用泛型创建工厂函数时,需要引用构造函数的类类型
53+
function create<T>(c: { new(): T; }): T {
54+
return new c();
55+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /