Skip to content

Commit 871d031

Browse files
author
chufan
committed
feat: update
1 parent 2a2f206 commit 871d031

File tree

5 files changed

+351
-2
lines changed

5 files changed

+351
-2
lines changed

code/node/typescript/demo-1.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 基础类型
3+
*/
4+
5+
export default {
6+
7+
}
8+
9+
// 布尔类型
10+
const isSuccess:boolean=true
11+
console.log(isSuccess)
12+
13+
// number类型
14+
const a:number=123
15+
console.log(a)
16+
17+
// string类型
18+
const name="chufan"
19+
console.log(name)
20+
21+
// 模板字符串
22+
const sister:string=`hello,${name}`
23+
console.log(sister)
24+
25+
26+
/* 数组*/
27+
const list:number[]=[1,2,3,4]
28+
const arrayList:Array<number>=[1,2,3,4]
29+
console.log(list,arrayList)
30+
31+
const tupleResult:[string,number]=['chufan',23]
32+
console.log(tupleResult)
33+
34+
/*枚举*/
35+
enum COLOR{
36+
RED=0,
37+
GREEN=1,
38+
BLUE=3
39+
}
40+
enum NAME{
41+
zhangSan='zhangSan',
42+
liSi='liSi'
43+
}
44+
45+
console.log(COLOR.BLUE,NAME.zhangSan)
46+
47+
48+
/*void*/
49+
// 函数返回值为空
50+
function testVoid(a:number):void{
51+
console.log(a)
52+
}
53+
const aVoid:void=null
54+
const bVoid:void=undefined
55+
console.log(aVoid,bVoid)
56+

code/node/typescript/demo-2.ts

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/**
2+
* 变量声明
3+
*/
4+
5+
export default {}
6+
7+
let a:number=1
8+
9+
// 函数内部定义
10+
function test():number{
11+
const a=1
12+
return a+2;
13+
}
14+
15+
16+
// 块级作用域
17+
function testResult(input: boolean) {
18+
let a = 100;
19+
20+
if (input) {
21+
// Still okay to reference 'a'
22+
let b = a + 1;
23+
return b;
24+
}
25+
26+
// Error: 'b' doesn't exist here
27+
// return b;
28+
}
29+
30+
// 数组解构构
31+
const result=[1,2]
32+
let [resultA,resultB]=result
33+
console.log(resultA,resultB)
34+
35+
36+
const res=[1,3,4,5,6]
37+
const [resA,...rest]=res
38+
console.log(resA,rest)
39+
40+
// 对象结构
41+
const boy={
42+
name:'chufan',
43+
gender:'man',
44+
age:14
45+
}
46+
const {name,age}=boy
47+
console.log(name,age)
48+
const {gender,...restBoy}=boy
49+
console.log(gender,restBoy)
50+
51+
const {defaultA,defaultB=1001}={defaultA:100}
52+
53+
54+
// 接口的函数类型
55+
interface SearchFunc{
56+
say:(name:string,age:number)=>string;
57+
sex:boolean;
58+
}
59+
60+
const searchTest:SearchFunc={
61+
sex:true,
62+
say:(name:string,age:number)=>{
63+
return name
64+
}
65+
}
66+
console.log(searchTest)
67+
68+
interface myFunc{
69+
(name:string):string
70+
}
71+
const func:myFunc=(name:string)=>{
72+
return name
73+
}
74+
console.log(func('储凡'))
75+
76+
77+
interface StrArray {
78+
// 索引为number类型,值为字符串类型
79+
[index: number]: string;
80+
}
81+
82+
let myArray: StrArray;
83+
myArray = ["储凡", "chufan"];
84+
85+
let myStr: string = myArray[0];
86+
87+
console.log(myStr)
88+
89+
90+
interface myInter{
91+
name:string
92+
sayHello():string
93+
}
94+
95+
class Inter implements myInter{
96+
name: string;
97+
98+
sayHello(): string {
99+
return ''
100+
}
101+
}
102+
103+
// 直接将属性定义在构造函数中
104+
class StudentA{
105+
constructor(
106+
public readonly name:string,
107+
public readonly age:number
108+
) {
109+
110+
}
111+
112+
async getName(){
113+
return this.name
114+
}
115+
116+
async getAge(){
117+
return this.age
118+
}
119+
}
120+
121+
// 等价于
122+
class StudentB{
123+
public readonly name:string
124+
public readonly age:number
125+
126+
constructor(name,age) {
127+
this.name=name
128+
this.age=age
129+
}
130+
131+
async getName(){
132+
return this.name
133+
}
134+
135+
async getAge(){
136+
return this.age
137+
}
138+
}
139+
140+
141+
// 类类型
142+
interface Plan{
143+
food:string
144+
eat:(something:string)=>boolean
145+
146+
}
147+
148+
class PlanA implements Plan{
149+
food: string;
150+
eat(something: string): boolean {
151+
return false;
152+
}
153+
}
154+
155+
interface FairyA{
156+
name:string
157+
}
158+
159+
interface FairyB{
160+
age:number
161+
}
162+
163+
interface Fairy extends FairyA,FairyB{
164+
gender:number
165+
}
166+
const fairy:Fairy={
167+
name:"储凡",
168+
age:18,
169+
gender:0
170+
}
171+
172+
console.log(fairy)
173+
174+
175+
class Dog{
176+
age:number
177+
}
178+
179+
class DogA extends Dog{
180+
name:string
181+
}
182+
183+
const dogA=new DogA()
184+
dogA.name='dogA'
185+
dogA.age=4
186+
187+
188+
class DogB extends Dog{
189+
private _gender:number
190+
get gender():number{
191+
return this._gender
192+
}
193+
set gender(gender:number):void{
194+
this._gender=gender
195+
}
196+
197+
198+
// 等价于
199+
public getGender():number{
200+
return this._gender
201+
}
202+
public setGender(gender:number):void{
203+
this._gender=gender
204+
}
205+
}
206+
207+
const dogB=new DogB()
208+
209+
// 通过gender方法来获取_gender私有属性值
210+
dogB.gender=18
211+
console.log(dogB.gender)
212+
213+
class TestStatic{
214+
public static age=18
215+
public static async test(){
216+
return 'this is using static function'
217+
}
218+
}
219+
220+
console.log(TestStatic.age)
221+
console.log(TestStatic.test())
222+
223+
224+
225+
class PointXY {
226+
x: number;
227+
y: number;
228+
}
229+
230+
interface PointXYZ extends PointXY {}
231+
232+
233+
234+
type PointX={
235+
x:number
236+
}
237+
238+
type PointY={
239+
y:number
240+
}
241+
242+
type PointZ={
243+
z:number
244+
}
245+
246+
type PointXYZ3 =PointX & PointY & PointZ
247+
const pointXYZ:PointXYZ3={
248+
x:1,
249+
y:2,
250+
z:3
251+
}
252+
console.log(pointXYZ)
253+
254+
type typeString=string
255+
type typeNumber=number
256+
257+
type newType=typeString | typeNumber
258+
259+
interface Bird {
260+
fly();
261+
layEggs();
262+
}
263+
264+
interface Fish {
265+
swim();
266+
layEggs();
267+
}
268+
269+
function getAnimal(): Fish | Bird {
270+
// ...
271+
272+
return {
273+
swim() {
274+
},
275+
layEggs() {
276+
}
277+
}
278+
}
279+
280+
let animal = getAnimal();
281+
animal.layEggs() // okay
282+
// animal.swim() // errors
283+
// animal.fly() // errors
284+
285+
// 断言为Bird类型
286+
if((<Bird>animal).fly !=null){
287+
(<Bird>animal).fly()
288+
}
289+
290+
if((<Fish>animal).swim!=null){
291+
(<Fish>animal).swim()
292+
}
293+
294+

docs/manuscript/server-end/typescript/简介.md

Whitespace-only changes.

docs/manuscript/solo-algorithm/interview-101/链表/1.reverseList.md

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

1111
### 题目描述
1212

13-
![反转链表.png](reverseList.png)
13+
![反转链表.png](./reverseList.png)
1414

1515

1616

donate.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)