typescript

2023-09-11 00:00    #typescript  

安装

1yarn global add typescript ts-node

数据类型

1function alerme():void {
2  alert('my name is tom');
3}
1let num:number = null
 1const xiaoJieJie : {
 2    name:string,
 3    age:number
 4}   = {
 5    name:'hel',
 6    age:19
 7} //1 普通
 8
 9const xiaoJjiejieS :string [] = ['1','dajiao','liuyaing']; // 2  .数组
10
11class Person{}
12const dajiao: Person = new Person() //3 类类型
13
14const jianXiaoJjieJie: () => string  = () => {return "dajiao"}; //函数对象类型

类型注解 与 类型推断

工作使用的原则

05 函数参数和返回类型的注解

1<%- include("demo/demo5.ts") %>

06数组类型的定义

1<%- include("demo/demo6.ts") %>

07 元组的使用

1<%- include("demo/demo7.ts") %>

元组现在用的少了,因为可以用对象的形式来代替

08 09 interface 接口

1<%- include("demo/demo8.ts") %>
1<%- include("demo/demo9.ts") %>

10 类的概念和使用

1<%- include("demo/demo10.ts") %>

类的访问类型

类的内部与外部

1<%- include("demo/demo11.ts") %>

12 类的构造函数

1<%- include("demo/demo12.ts") %>

13 类的getter setter static 只读

1<%- include("demo/demo13.ts") %>

14 抽象类

1<%- include("demo/demo14.ts") %>

15 16 17 tsconfig.json 配置文件

compileOptions

18 类型保护与联合类型

1<%- include("demo/demo18.cpp") %>

19 enum 类型 typescript 独有

1<%- include("demo/demo19.cpp") %>

20 泛型

1<%- include("demo/demo20.cpp") %>

21 类中使用泛型

 1
 2 - 泛型在类中的使用 `class C<T>`
 3
 4为了约束传入的参数
 5
 6 - 泛型继承
 7 - 泛型约束
 8
 9<%- include("demo/demo21.cpp") %>
10
11## 22 23 命名空间
12
13在浏览器中运行与看效果 
14
15 - vscode -> file -> openFloder
16 - vscode->terminal `npm init`
17 - `tsc -init`
18 - `mkdir src && mkdir build && touch index.html`
19
20 - 命名空间可以嵌套

namespace fater { namespace child { } }

1
2tsconfig.json
3 - outFile ->生成一个单独的文件
4 - moduble coomjs ->'amd'
5
6## 24 import
7
8需要引用 require.js 来使用 amd 的模块

require([“page”],function(){ })

1
2## 25 parcel

yarn add –dev parcel

1
2## 26 ts 使用jquery

yarn add @types/jquery

1
2or

declare var $:any;

1
2## 资料
3
4 - [ruan一峰 es6]()
5 - [TypeScript 入门教程](https://ts.xcatliu.com/)
6 - [TypeScript Handbook(中文版)](https://zhongsp.gitbooks.io/typescript-handbook/content/)
7 - [技术胖-TypeScript 从入门到精通图文视频教程-免费教程](http://jspang.com/detailed?id=63)