Skip to content

Commit be7425f

Browse files
author
142vip.cn
committed
chore: update
1 parent 6b33bf2 commit be7425f

File tree

11 files changed

+831
-0
lines changed

11 files changed

+831
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TypeScript
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export const TypescriptSidebar = [
2+
{
3+
text: '基础教程',
4+
prefix: '基础教程',
5+
children: [
6+
{
7+
text: '快速入门',
8+
link: '快速入门.md'
9+
},
10+
{
11+
text: '基础类型',
12+
link: '基础类型.md'
13+
},
14+
{
15+
text: '变量声明',
16+
link: '变量声明.md'
17+
},
18+
{
19+
text: '接口',
20+
link: '接口.md'
21+
},
22+
{
23+
text: '泛型',
24+
link: '泛型.md'
25+
},
26+
{
27+
text: '类',
28+
link: '类.md'
29+
},
30+
{
31+
text: '高级类型',
32+
link: '高级类型.md'
33+
},
34+
{
35+
text: '装饰器',
36+
link: '装饰器.md'
37+
}
38+
]
39+
}
40+
]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 变量声明
2+
3+
const是对let的一个增强,它能阻止对一个变量再次赋值
4+
5+
## 块级作用域
6+
7+
```ts
8+
// 块级作用域
9+
function testResult(input: boolean) {
10+
let a = 100;
11+
12+
if (input) {
13+
// 这里能获取到变量a的值
14+
let b = a + 1;
15+
return b;
16+
}
17+
18+
// 编译器会提醒变量不存在
19+
return b;
20+
}
21+
```
22+
23+
## 数组解构
24+
25+
```ts
26+
const result = [1, 2]
27+
const [resultA, resultB] = result
28+
console.log(resultA, resultB)
29+
30+
// 数据交换
31+
// [resultA, resultB] = [resultB, resultA]
32+
33+
// 对剩余变量的获取
34+
const res = [1, 3, 4, 5, 6]
35+
const [resA, ...rest] = res
36+
console.log(resA, rest)
37+
```
38+
39+
## 对象解构
40+
41+
```ts
42+
const boy = {
43+
name: 'chufan',
44+
gender: 'man',
45+
age: 14
46+
}
47+
const {name, age} = boy
48+
console.log(name, age)
49+
const {gender, ...restBoy} = boy
50+
console.log(gender, restBoy)
51+
52+
// 解构变量重命名
53+
const {name: newName} = boy
54+
console.log(nerName)
55+
56+
// 默认值
57+
const {defaultA, defaultB = 1001} = {defaultA: 100}
58+
```
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 基础类型
2+
3+
## 布尔类型
4+
5+
```ts
6+
// 布尔类型
7+
const isSuccess: boolean = true
8+
console.log(isSuccess)
9+
```
10+
11+
## 数字
12+
13+
`JavaScript`一样,`TypeScript`里的所有数字都是浮点数。 这些浮点数的类型是 number
14+
15+
```ts
16+
// number类型
17+
const a: number = 123
18+
console.log(a)
19+
```
20+
21+
## 字符串
22+
23+
```ts
24+
// string类型
25+
const name = "chufan"
26+
console.log(name)
27+
28+
// 模板字符串
29+
const sister: string = `hello,${name}`
30+
console.log(sister)
31+
```
32+
33+
## 数组
34+
35+
`TypeScript``JavaScript`一样可以操作数组元素。 有两种方式可以定义数组:
36+
37+
- 可以在元素类型后面接上 `[]`,表示由此类型元素组成的一个数组
38+
- 使用数组泛型,`Array<元素类型>`
39+
40+
```ts
41+
// 数组
42+
const list: number[] = [1, 2, 3, 4]
43+
const arrayList: Array<number> = [1, 2, 3, 4]
44+
console.log(list, arrayList)
45+
```
46+
47+
## 元组
48+
49+
元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同
50+
51+
```ts
52+
const tupleResult: [string, number] = ['chufan', 23]
53+
console.log(tupleResult)
54+
```
55+
56+
## 枚举
57+
58+
```ts
59+
// 枚举颜色
60+
enum COLOR {
61+
RED = 0,
62+
GREEN = 1,
63+
BLUE = 3
64+
}
65+
66+
// 枚举人名
67+
enum NAME {
68+
zhangSan = 'zhangSan',
69+
liSi = 'liSi'
70+
}
71+
72+
console.log(COLOR.BLUE, NAME.zhangSan)
73+
```
74+
75+
## Void和Any
76+
77+
某种程度上来说,void类型像是与any类型相反。
78+
79+
- void表示没有任何类型。 当一个函数没有返回值时,你通常会见到其返回值类型是 void
80+
- any表示任意类型,一般写any就直接绕过ts编译等同于js代码
81+
82+
```ts
83+
// 函数返回值为空
84+
function testVoid(a: number): void {
85+
console.log(a)
86+
}
87+
88+
// 声明一个void类型的变量没有什么大用,你只能为它赋予undefined和null
89+
const aVoid: void = null
90+
const bVoid: void = undefined
91+
console.log(aVoid, bVoid)
92+
```
93+
94+
## Null 和 Undefined
95+
96+
TypeScript里,undefined和null两者各自有自己的类型分别叫做undefined和null。 和 void相似,它们的本身的类型用处不是很大:
97+
98+
```ts
99+
let u: undefined = undefined;
100+
let n: null = null;
101+
```
102+
103+
默认情况下null和undefined是所有类型的子类型。 就是说你可以把 null和undefined赋值给number类型的变量。
104+
然而,当指定了--strictNullChecks标记,null和undefined只能赋值给void和它们各自。 这能避免 很多常见的问题
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: 简介
3+
permalink: /manuscripts/server-end/typescript/quick-start.html
4+
---
5+
6+
# 快速入门
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# 接口
2+
3+
接口的作用就是为这些类型命名和为代码或第三方代码定义契约
4+
5+
接口可以限制类的结构
6+
7+
```ts
8+
// 定义接口类型
9+
interface LabelledValue {
10+
label: string
11+
}
12+
13+
// 定义函数
14+
function printLabel(labelledObj: LabelledValue) {
15+
console.log(labelledObj.label);
16+
}
17+
18+
let labelObj = {size: 10, label: "Size 10 Object"}
19+
printLabel(labelObj)
20+
21+
// 可选属性
22+
interface SquareConfig {
23+
color?: string
24+
width?: number
25+
}
26+
27+
// 只读属性
28+
interface Point {
29+
readonly x: number;
30+
readonly y: number;
31+
}
32+
33+
// 接口中的所有属性都不能有实际的值
34+
// 接口只定义对象的结构,不考虑实际的值
35+
// 在接口中,所有的方法都是抽象方法
36+
interface myInter {
37+
name: string
38+
39+
sayHello(): string
40+
}
41+
42+
class Inter implements myInter {
43+
name: string;
44+
45+
sayHello(): string {
46+
return ''
47+
}
48+
}
49+
```
50+
51+
对于只读,const支持变量的只读,readonly支持属性的只读
52+
53+
- 抽象类中可以有抽象方法和普通方法,抽象方法就是需要自己实现的方法
54+
- 接口中只有抽象方法,对方法进行约束定义。使用implements后,实现抽象方法
55+
56+
## 函数类型
57+
58+
```ts
59+
60+
// 接口的函数类型
61+
interface SearchFunc {
62+
say: (name: string, age: number) => string;
63+
sex: boolean;
64+
}
65+
66+
const searchTest: SearchFunc = {
67+
sex: true,
68+
say: (name: string, age: number) => {
69+
return name
70+
}
71+
}
72+
console.log(searchTest)
73+
74+
75+
// 通过接口来约束函数类型
76+
interface myFunc {
77+
(name: string): string
78+
}
79+
80+
const func: myFunc = (name: string) => {
81+
return name
82+
}
83+
console.log(func('储凡'))
84+
```
85+
86+
## 可索引类型
87+
88+
指定索引类型,例如,指定对象key的类型
89+
90+
```ts
91+
interface StrArray {
92+
// 索引为number类型,值为字符串类型
93+
[index: number]: string;
94+
}
95+
96+
let myArray: StrArray;
97+
myArray = ["储凡", "chufan"];
98+
99+
let myStr: string = myArray[0];
100+
101+
console.log(myStr)
102+
```
103+
104+
## 类类型
105+
106+
```ts
107+
// 类类型
108+
interface Plan {
109+
food: string
110+
eat: (something: string) => boolean
111+
112+
}
113+
114+
// 通过接口来约束类的属性、方法
115+
class PlanA implements Plan {
116+
food: string;
117+
118+
eat(something: string): boolean {
119+
return false;
120+
}
121+
}
122+
```
123+
124+
## 继承接口
125+
126+
```ts
127+
interface FairyA {
128+
name: string
129+
}
130+
131+
interface FairyB {
132+
age: number
133+
}
134+
135+
interface Fairy extends FairyA, FairyB {
136+
gender: number
137+
}
138+
139+
const fairy: Fairy = {
140+
name: "储凡",
141+
age: 18,
142+
gender: 0
143+
}
144+
145+
console.log(fairy)
146+
```

0 commit comments

Comments
 (0)