1. 常用类型注解
let age: number = 20;
JS已有:
- 原始类型:number、string、boolean、null、undefined、symbol
- 对象类型:object (包括:数组、对象、函数等对象)
TS新增:
- 联合类型、自定义类型(类型别名)、接口、元组、字面量类型、枚举、void、any 等
注:类型别名和接口的区别:
- 类型别名可以指定约束任意类型
- 接口只能指定约束对象类型
| 数组类型两种写法: |
| let num: number[] = [1,2,3] |
| let str: Array<string> = ['a','b','c'] |
| |
| 联合类型: |
| let arr: number|string = 'abc' |
| let arr: number|string[] = 1 |
| let arr: (number|string)[] = [1,'a'] |
| |
| 类型别名: |
| type CustomArray = (number|string)[] |
| let arr: CustomArray = [1,'a'] |
| |
| 函数类型: |
| function fn(num: number):number { |
| return num |
| } |
| |
| function fn(num?: number):void { |
| console.log(num) |
| } |
| |
| 对象类型: |
| let per:{ name: string; age: number; say():void } = { |
| name: 'zzz', |
| ahe: 20, |
| say(){ } |
| } |
| |
| 接口: |
| interface Person { |
| name: string |
| age: number |
| } |
| |
| let per: Person = {...} |
| |
| 接口继承: |
| interface P1 = { x:number; y:number; } |
| interface P2 extends = { z:number } |
| |
| 元组类型: |
| let position: [number, number] = [1,2] |
| |
| 类型推论: |
| let num = 2 |
| |
| lat num |
| num = 2 |
| |
| 字面量类型: |
| const name = "abc" |
| let name: ( "a" | "b" | "c" ) = "a" |
| |
| 枚举: |
| enum Name { a, b, c } |
| let name: Name = b |
| console.log(Name.a+Name.b+Name.c) |
| enum P { a=2, b, c } |
| enum P { a="a",b="b",c="c" } |
| |
| any类型: |
| let obj:any = { x=0 } |
| obj() |
| obj.a=1 |
| |
| TS中的typeof操作符: |
| let P = { x:1,y:2 } |
| function fn(point:P){ } |
2. 高级注解类型
- class 类
- 类型兼容性
- 交叉类型
- 泛型 和 keyof
- 索引签名类型、索引查询类型
- 映射类型
2.1 class类
| class Person { |
| age = 20 |
| } |
| const p = new Person() |
| p.age |
| class Person { |
| age: number |
| gender: string |
| |
| constructor(age: number, gender: string) { |
| this.age = age |
| this.gender = gender |
| } |
| |
| ageAdd(n: number) { |
| this.age += n |
| } |
| } |
| |
| const p = new Person(20,'男') |
| p.ageAdd(10) |
2.2 类的继承和实现接口
| class Animal { |
| move() { console.log('Moving along!') } |
| } |
| |
| class Dog extends Animal { |
| bark() { console.log('汪') } |
| } |
| |
| const dog = new Dog() |
| |
| interface Singable { |
| sing(): void |
| } |
| |
| |
| class Person implements Singable { |
| |
| sing() { |
| console.log('wowowowo') |
| } |
| } |
2.3 public protected private
- public 均可访问
- protected 类内和子类可以访问,但实例不可
- private 仅类内可访问
2.4 readonly 只读类型
| class Person { |
| readonly age: number = 20 |
| constructor(age: number) { |
| this.age = age |
| } |
| } |
| |
| let p = new Person(18) |
| console.log(p.age) |
| p.age = 22 |
2.5 类型兼容性




2.6 交叉类型
| interface Person { name: string } |
| interface Contact { phone: string } |
| type PersonDetail = Person & Contact |
2.7 泛型
| |
| function fn<Type>(n: Type):Type { return n } |
| |
| const f1 = fn<number>(20) |
| const f2 = fn<string>('abc') |
| function fn<Type>(n: Type[]):Type[] { |
| console.log( n.length ) |
| } |
| |
| |
| interface Length { length: number } |
| |
| function fn<Type extends Length>(n: Type):Type { |
| console.log(n.length) |
| } |
| function getProp<Type, Key extends keyof Type>(objL Type, key: Key) { |
| return obj[key] |
| } |
| |
| getProp({ name: 'zrh', age: 20 }, 'age') |
| getProp({ name: 'zrh', age: 20 }, 'name') |
| |
| interface IdFn<Type> { |
| id:(value: Type) => Type |
| ids:() => Type[] |
| } |
| |
| |
| let obj: IdFn<number> = { |
| id(value) { return value } |
| ids() { return [1,2,3] } |
| } |
2.8 泛型工具类




【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)