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 e26696a

Browse files
author
febobo
committed
update typesciprt.md
1 parent b1f558f commit e26696a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

‎docs/es6/typeScript.md‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### 基本语法
1+
### 概述
22

33
### 变量申明
44
```js
@@ -30,6 +30,51 @@ const point: Point = { x: 10, y: 20 }
3030
```js
3131
type mathfunc = (a: number, b: number) => number
3232
const product: mathfunc = (a, b) => a * b
33+
```
34+
35+
### 泛型
36+
泛型的意义在于函数的重用性,我们希望组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型
37+
38+
**为什么不用`any`呢?**
39+
40+
使用 `any` 会丢失掉一些信息,我们无法确定返回值是什么类型
41+
泛型可以保证入参跟返回值是相同类型的,它是一种特殊的变量,只用于表示类型而不是值
42+
43+
语法 `<T>(arg:T):T` 其中`T`为自定义变量,能前后对应就行
44+
45+
```js
46+
const hello : string = "Hello vue!"
47+
function say<T>(arg: T): T {
48+
return arg;
49+
}
50+
console.log(say(hello)) // Hello vue!
51+
```
52+
53+
#### 泛型约束
54+
我们使用同样的例子,加了一个`console`,但是很不幸运,报错了,因为泛型无法保证每种类型都有`.length` 属性
55+
56+
```js
57+
const hello : string = "Hello vue!"
58+
function say<T>(arg: T): T {
59+
console.log(arg.length) // Property 'length' does not exist on type 'T'.
60+
return arg;
61+
}
62+
console.log(say(hello)) // Hello vue!
63+
```
64+
65+
从这里我们也又看出来一个跟`any`不同的地方,如果我们想要在约束层面上就结束战斗,我们需要定义一个接口来描述约束条件
66+
67+
```js
68+
interface Lengthwise {
69+
length: number;
70+
}
71+
72+
function say<T extends Lengthwise>(arg: T): T {
73+
console.log(arg.length)
74+
return arg;
75+
}
76+
console.log(say(1)) // Argument of type '1' is not assignable to parameter of type 'Lengthwise'.
77+
console.log(say({value: 'hello vue!', length: 10})) // { value: 'hello vue!', length: 10 }
78+
```
79+
3380

34-
console.log(num, str, arr, set, map, sum(1, 2), product(2, 3), point)
35-
```

‎docs/reactivity/effect.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### 正文
1010

11-
我们还是选择先从定义看起,慢慢的跟我一起扯下它神秘的面纱
11+
我们还是选择先从定义看起
1212

1313
`effect` 接收两个参数
1414
- `fn` 回调函数

0 commit comments

Comments
(0)

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