File tree 9 files changed +133
-1
lines changed
9 files changed +133
-1
lines changed Original file line number Diff line number Diff line change 7
7
# typescript 进阶 ignore
8
8
/typescript进阶 /node_modules
9
9
/typescript进阶 /build
10
+ /typescript进阶 /dist
10
11
/typescript进阶 /package-lock.json
12
+ /typescript进阶 /.parcel-cache
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
+ < title > Document</ title >
7
+ </ head >
8
+ < body >
9
+ </ body >
10
+ <!-- <script src="./src/2-8parcel编辑运行.ts"></script> -->
11
+
12
+ < script src ="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.js "> </ script >
13
+ < script src ="./src/2-9描述文件的全局类型.ts "> </ script >
14
+ < script src ="./src/2-11泛型中keyof的语法.ts "> </ script >
15
+ < script >
16
+
17
+ </ script >
18
+ </ html >
Original file line number Diff line number Diff line change 5
5
"main" : " index.js" ,
6
6
"scripts" : {
7
7
"dev" : " tsc -w" ,
8
- "test" : " ts-node ./src/2-3Enum枚举类型.ts"
8
+ "test" : " ts-node ./src/2-3Enum枚举类型.ts" ,
9
+ "parcel" : " parcel ./index-parcel.html"
9
10
},
10
11
"keywords" : [],
11
12
"author" : " " ,
12
13
"license" : " ISC" ,
13
14
"devDependencies" : {
15
+ "parcel" : " ^2.0.0-alpha.3.2" ,
14
16
"ts-node" : " ^8.9.1" ,
15
17
"typescript" : " ^3.8.3"
18
+ },
19
+ "dependencies" : {
20
+ "jquery" : " ^3.5.1"
16
21
}
17
22
}
Original file line number Diff line number Diff line change
1
+ import $ from 'jquery' ;
2
+
3
+ $ ( ( ) => {
4
+ $ ( 'body' ) . html ( '<div>12hah34</div>' )
5
+ } )
Original file line number Diff line number Diff line change
1
+ interface Person {
2
+ name : string ;
3
+ age : number ;
4
+ gender : string ;
5
+ }
6
+
7
+ /**
8
+ * 用extend的语法
9
+ * keyof进行属性遍历,
10
+ * T extends keyof Person
11
+ * 相当于
12
+ * type name = "name";
13
+ * T = name;
14
+ * type age = "age";
15
+ * T = age;
16
+ * type gender = "gender";
17
+ * T = gender;
18
+ */
19
+ class Teacher {
20
+ constructor ( private info : Person ) {
21
+
22
+ }
23
+ // getInfo(key: string) {
24
+ // if (key === 'name' || key === "age" || key === 'gender'){
25
+ // return this.info[key]
26
+ // }
27
+ // }
28
+ getInfo < T extends keyof Person > ( key : T ) :Person [ T ] {
29
+ return this . info [ key ]
30
+ }
31
+ }
32
+
33
+ const teacher1 = new Teacher ( {
34
+ name : "dell" ,
35
+ age : 12 ,
36
+ gender : 'mode'
37
+ } )
38
+
39
+ const test = teacher1 . getInfo ( 'name' ) ;
40
+ console . log ( test )
Original file line number Diff line number Diff line change
1
+ // parcel的中文网址: https://parceljs.org/
2
+ // 几乎是0配置
3
+ const student :string = 'guoguo' ;
4
+ console . log ( student )
Original file line number Diff line number Diff line change
1
+ let aaa : string = '111' ;
2
+ console . log ( aaa ) ;
3
+
4
+ $ ( ( ) => {
5
+ console . log ( '123' )
6
+ new $ . fn . init ( ) ;
7
+ } )
8
+
9
+
10
+ $ ( ( ) => {
11
+ $ ( 'body' ) . html ( '<div>12hah34</div>' )
12
+ } )
13
+
14
+ $ ( function ( ) {
15
+ $ ( 'body' ) . html ( '<div>12hah34</div>' )
16
+ } )
Original file line number Diff line number Diff line change
1
+ // 定义全局变量
2
+ // declare var $:(params:() => void) => void;
3
+
4
+ interface JqueryInstance {
5
+ html : ( html : string ) => JqueryInstance ;
6
+ }
7
+
8
+ // 定义全局函数
9
+ // 函数重载
10
+ declare function $ ( readFunc : ( ) => void ) :void ;
11
+ declare function $ ( selector :string ) :JqueryInstance ;
12
+
13
+ // 全局如何对对象进行定义,以及如何对类进行定义,以及命名空间的嵌套
14
+ declare namespace $ {
15
+ namespace fn {
16
+ class init { }
17
+ }
18
+ }
19
+
20
+
21
+
22
+ // 使用接口的方式实现函数重载
23
+ // interface JQuery{
24
+ // (readyFunc:() => void): void
25
+ // (selector: string): JqueryInstance
26
+ // }
27
+ // declare var $: JQuery;
28
+
Original file line number Diff line number Diff line change
1
+ // npm 模块化声明
2
+ declare module 'jquery' {
3
+ interface JqueryInstance {
4
+ html : ( html : string ) => JqueryInstance ;
5
+ }
6
+ function $ ( readyFunc : ( ) => void ) : void ;
7
+ function $ ( selector : string ) : JqueryInstance ;
8
+ namespace $ {
9
+ namespace fn {
10
+ class init { }
11
+ }
12
+ }
13
+ export = $ ;
14
+ }
You can’t perform that action at this time.
0 commit comments