Ts笔记
一、类型推断
发生在没有指定类型时,会根据初始值自动推断变量类型
1 2
| let str = 'asdasdasds' str = 0
|
二、类型断言/注解 as
有些时候编译器不能推断出变量的类型,
但我们非常确定变量是某种类型时 : as number/string/...
1 2 3 4
| let arr = [1,2,3] const result = arr.find(item=> { item> 2 }) as number
|
html元素
1 2 3 4 5 6 7 8
|
const a = document.querySelector('#id') as HTMLAnchorElement a && a.href
|
三、类型基础&联合类型&函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| let v1: string = 'asd' let v2: number = 123 let v3: boolean = true let v4: null = null let v5: undefined = undefined let v6: symbol = Symbol(123) let v7: bigInt = BigInt("223565679085")
let str: string | null = null let num: 1 | 2 | 3 = 2; let arr: (number | string)[] = [1,2,'asd']
function MyFn(a: number, b: string):void { return a+b } function MyFn(a: number, b: string, C?: boolean, ...rest: number[]):string { return a+b } const f = MyFn(20, 'a', true, 1,2,3,4)
|
复杂类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| let arr: number[] = [1, 2, 3] let arr1: Array<string> = ['a', 'b', 'c']
let tup: [number, string, number?] = [1, 'a', 2] tup[0] = 'a'
enum MyEnum { 'up', 'down', 'left', 'right' } MyEnum.up MyEnum[0]
enum MyEnum { up = 100, down }
enum MyEnum { up = 'UP' } MyEnum.up MyEnum['up']
|
接口
- interface 只能定义对象, 也可以是实例对象
- 必须实现接口里面的所有内容,且不能多不能少
- 定义对象优先interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| interface Person { name: string, age: number }
interface Itman extends Person { skill: string[],
} const p1: Itman = { name: 'blank', age: 21, skill: ['js','vue'] }
|
泛型接口
1 2 3 4 5 6 7 8 9
| interface Student<T> { id: T name: string } let stu : Student<number> = { id: 0x110, name: 'blank' } console.log(stu);
|
别名 type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| type b = 0 | 1
type MyType = string | number type myType2 = string & number
type fnType = (a: number, bb: number)=> number let fn: fnType = (a, b)=> { return a+b }
type student = { name: string, height: number, girlfriend?: student, study: (subject: string) => string, back: ()=>void } let stu: student = {...} stu.girlfriend?.name
|
泛型 <T>
基本使用
1 2 3 4 5 6
| function myFn<T>(a: T, b: T): T[] { return [a,b] } myFn<number>(1,2) myFn('a',2)
|
添加约束
- 泛型T可以继承接口,必须实现接口所有属性、方法,可以超出接口没有的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| interface ILength { length: number, sayHi: () => void, }
function getId<T extends ILength>(val: T) { val.length val.sayHi() }
let so = { length: 123, sayHi() { console.log("sayhi") }, father: 'lzy' } getId(so)
|
多个变量
K extends Keyof T
即 Son类型限定了 传入的变量只能是Father类型的键
1 2 3 4 5
| function getProp<T, K extends keyof T>(obj: T, key: K) { return obj[key] } console.log(getProp({name: 'lzy'}, 'name')) console.log(getProp({name: 'lzy'}, 'age'))
|
class中使用ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| interface ILength { length: number, sayHi: () => void, }
class A implements ILength,B{ length: number; constructor(length:number) { this.length = length } sayHi() { console.log('hi'); } } let a = new A(123)
|
修饰符
- public: 公有的可以在任何地方可以被访问和修改(类中属性和方法默认为publish)
- private: 私有的仅在当前类可访问和修改
- protected: 仅当前类与子类(继承类)可以访问和修改
- readonly: 只读修饰符, 仅可访问不可修改
any 类型
- 失去了只能提示
- 隐式any:
- 函数参数不定义类型
- 定义变量不定义类型也不给初始值
运行ts文件
tsc demo.ts
# 生成一个demo.js 文件
1 2 3 4 5 6
|
2. 在node环境执行
```bash ts-node demo.ts
|