typescript

3,358 阅读3分钟

node 同时运行多个服务 concurrently

"dev:build": "tsc -w",
"dev:start": "nodemon node ./build/index.js",
"dev": "tsc && concurrently npm:dev:*"
ts-node 执行ts文件,相当于node 执行js文件
在浏览器中调试node项目,使用nodemon --inspect index.js
processon 绘制流程图
yeoman 脚手架

ts约束class静态类型(包括构造函数类型)

//约束静态类型
interface ClockInterface {//约束实例类型
    currentTime: number;
    alert(): void;
}
interface ClockStatic { //约束静态类型
    new (h: number, m: number): void;
    time: number
}
const Clock:ClockStatic = class Clock implements ClockInterface {
    constructor(h:number, m: number) {

    }
    static time = 12;
    currentTime: number = 123;
    alert() {

    }
}
//implements后面也可以跟多个interface
//实例上可以设置public private protected
//protected 只能自己和父级使用

ts约束class实例类型

// implements 定义类实现接口的方式,外面使用类的实例方法, 
//implements可以实现多个接口
interface AlertType {
    (name: string): void;
}

interface GetAgeType {
    (age: number): void
}

interface Alarm {
    alert: AlertType
}

interface GetAge {
    getAge: GetAgeType
}


}
class SecurityDoor extends Door implements Alarm, GetAge {
    alert: AlertType = () => {
        console.log(8)
    }

    getAge(age: number) {

    }

    getCity() {
        this.alert('asd')
    }

}

抽象类

  • 抽象类不能被实例化
  • 抽象类的子类必须实现抽象类里面的抽象方法
  • 抽象类主要解决在父类定义的方法,每个子类实现的方法不一样,主要是定义一种规范,让每个子类去实现这些方法
// 像这些图形类,都有获取面积的方法,这个时候就可以抽出来一个抽象类
abstract class Geom {
  /**
  * 在 getArea 前面加一个 abstract,说明这个类是抽象的
  * 因为圆形,矩形,三角形的面积求解方式都不一样,它们都有去求面积的方法
  * 但是实现方式不一样,就是你也不知道怎么实现
  * 这样就是一个抽象方法
  */
  abstract getArea(): number;
}
/**
* 抽象类只能被继承,不能被实例化,比如 new Geom,
* 如果是抽象类,子类必须自己实现一下
*/
class Circle extends Geom{
  getArea(){
    return 123
  }
}

const app = new Circle()

typescript内置类型

interface Todo {
    title: string,
    age: number
}
type Std = Omit<Todo, 'title'>
//Omit去除title属性申明
//Pick找需要的属性声明
type TodoPreview = Pick<Todo, "title" | "completed">;
//Partial把属性全部变为可选
//Required把属性全部变为不可选
//Readonly全部变为可读
//Record
type Record<K extends keyof any, T> = {//源码
    [P in K]: T;
};

type Coord = Record<'x' | 'y', number>;
// 等同于
type Coord = {
    x: number;
    y: number;
}

// 对象常见操作
interface TextComponentProps {
  name: string,
  age: number,
}
interface PropToForm {
  text: string,
  uuid: number
}
type PropsToForms = {
  [P in keyof TextComponentProps]?: PropToForm
}
const obj: PropsToForms = {
  name: {text: '文本', uuid: 123},
}

//接口配合数组使用
interface TextComponentProps {
  name: string,
  age: number,
}
const res: TextComponentProps[] = [
  {name: 'aaa', age: 22}
]

interface Person {
    name: string
    age: number
}

type K = keyof Person
`keyof`获取键, T[k] 获取类型,相当于数组中获取值
`in`操作符,主要用于遍历keyof返回的键,
[P in keyof TextComponentProps]?: PropToForm 典型示例
  • extends
type isString<T> = T extends string ? true : false 
type T1 = isString<number> // false 
type T2 = isString<string> // true
props: { [key: string]: any };  //对象的键,和值,定义类型。

泛型约束


/* 泛型约束 */
//上例中,我们使用了 `extends` 约束了泛型 `T` 必须符合接口 `Lengthwise` 的形状,
也就是必须包含 `length` 属性。
interface Lengthwise {
    length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
    console.log(arg.length);
    return arg;
}
//泛型参数的默认类型
function createArray<T = string>(length: number, value: T): Array<T> {
    let result: T[] = [];
    for (let i = 0; i < length; i++) {
        result[i] = value;
    }
    return result;
}
// 泛型类
class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber<number>();

as const

/* 
as const相当于把类型变成只读的属性
第一种情况:let 申明的基本数据类型用as const断言,变得不能修改
as const 作用于对象,那么对象的属性全部变得不可读
as const 作用域元祖也一样
 */
let str = '你好啊' as const
const obj = {city: '成都'} as const

is关键字

//这里把str约束为string,在下面才能调用slice方法
const isString = (str: any): str is string => {
    return typeof str === 'string'
}

;(function name(x: unknown) {
    if(isString(x)) {
        x.slice(0)
    }
})('11')

参考资料1

参考资料2