js css html

ts语法概览

2023-02-28  本文已影响0人  海豚先生的博客

ts对应js的数据类型

let name: string = "bob";
let age: number = 37;
let isDone: boolean = false;
let u: undefined = undefined;
let n: null = null;
let obj: object = {x: 1};
let bigLiteral: bigint = 100n;
let sym: symbol = Symbol("me");

Array

// 元素类型[]
let list: number[] = [1, 2, 3];
// Array<元素类型>
let list: Array<number> = [1, 2, 3];

interface MyObject {
   name: string;
   age: number;
}

let arr: MyObject[] = [{name: "兔兔", age: 18}] // OK
复制代码

Tuple

let x: [string, number];

x = ['hello', 10]; // OK
x = [10, 'hello']; // Error

undefined和null

void

any和unknown

never

as 类型断言

// as 语法
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;

!

跳过ts校验,保证有值

// 断言event.target有值,不为null/undefined
const a:string = event.target!.name

类型推论

let myFavoriteNumber = 'seven';
myFavoriteNumber = 7; // Error

| 联合类型

let myFavoriteNumber: string | number;
myFavoriteNumber = 'seven'; // OK
myFavoriteNumber = 7; // OK

& 交叉类型

interface A {
  name: string,
  age: number
}
interface B {
  name: string,
  gender: string
}

let a: A & B = { // OK
    name: "兔兔",
    age: 18,
    gender: "男"
};

interface 接口

interface LabeledValue {
  label: string;
  money?: number; // 可选属性
  readonly x: number; // 只读属性
  [key: string]: any; // 索引签名,TypeScript 支持两种索引签名:字符串和数字。
}
function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}
// myObj中的属性会进行类型推论,以此绕过多余的类型检查
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj); // OK

ReadonlyArray

let a: number[] = [1, 2, 3, 4];
let ro: ReadonlyArray<number> = a;
ro[0] = 12; // Error
ro.push(5); // Error
ro.length = 100; // Error

a = ro; // Error
a = ro as number[]; // OK

extends 接口继承接口

interface Shape {
  color: string;
}
interface PenStroke {
  penWidth: number;
}
interface Square extends Shape, PenStroke {
  sideLength: number;
}

let square: Square = { sideLength: 1 } // Error
let square1: Square = { sideLength: 1, color: 'red' } // Error
let square2: Square = { sideLength: 1, color: 'red', penWidth: 2 } // OK

Pick 选择性继承

// 原始类型
interface TState {
    name: string;
    age: number;
    like: string[];
}
// 如果我只想要name和age
interface TSingleState extends Pick<TState, "name" | "age"> {};
或者
interface TSingleState extends Pick<TState, "name" | "age"> ;

函数声明

// 剩余参数
function sum(x: number, y: number, ...rest: any[]): number {
    return x + y;
}

函数表达式

// 可选参数、参数默认值
let mySum: (x: number, y: number, m:number = 11, z?: number) => number = function (x: number, y: number): number {
    return x + y;
};

用接口定义函数类型

interface SearchFunc{
  (source: string, subString: string): boolean;
}

let mySearch: SearchFunc = function(source: string, subString: string) { // OK
  let result = source.search(subString);
  return result >-1;
};

内置对象

let s: String = new String('兔神');
let n: Number = new Number(123);
let b: Boolean = new Boolean(1);
let e: Error = new Error('Error occurred');
let d: Date = new Date();
let r: RegExp = /[a-z]/;

let body: HTMLElement = document.body;
let allDiv: NodeList = document.querySelectorAll('div');
document.addEventListener('click', function(e: MouseEvent) {
  // Do something
});

function sum() {
    let args: IArguments = arguments; // 类数组对象
}

type 类型别名

type StringType = string;
let str: StringType;
str = 'hello';
str = 123 // Error

import type ... from & export type ... from

导入或者导出类型文件,编译器将把这些类型文件删除


// src/lib-type-re-export.ts
export type { Track, Playlist } from "./types"; // 类型文件
export type { CreatePlaylistRequestParams } from "./api"; // 类型文件
export { createPlaylist } from "./api";

// 会被编译为:

// dist/lib-type-re-export.js
export { createPlaylist } from "./api";

字符串字面量类型

type Name = 'ALisa' | 'Bob' | 'Cola'

let name: Name = 'Alisa'; // Error 与 DOM 中的全局 window 对象下的 name 属性出现了重名
let name1: Name = 'ALisa'; // OK
let name2: Name = 'Bob'; // OK
let name3: Name = 'Cola'; // OK
let name4: Name = '兔兔'; // Error

Enum 枚举

// 数字枚举
enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
console.log(Days.Sun) // 0
console.log(Days.Mon) // 1
// 初始化枚举成员,那么该初始化成员后面的成员会在它的基础上自动增长1
enum Days {Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat};
console.log(Days.Sun) // 1
console.log(Days.Mon) // 2

// 字符串枚举
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}
// 异构枚举,枚举可以混合字符串和数字成员
enum Direction {
    name = '兔兔',
    age = 18
}

class 类

class Animal {
    // public 修饰符,类的属性、方法可以在外部访问
    public static age: number = 18;
    // 类的属性、方法不可以在外部访问
    private static title: string = '兔兔';
    // protected 修饰符,在派生类中仍然可以访问(继承中可以访问)
    protected title: string = '兔兔';
    class Animal {
    constructor(public name: string, private age: number, protected sex: string) {
    
    }
}

Animal.age; // OK
Animal.title; // Error

abstract 抽象类

abstract class Department {
    constructor(public name: string) {
    }
    printName(): void {
        console.log('Department name: ' + this.name);
    }
    abstract printMeeting(): void; // 必须在派生类中实现
}
class AccountingDepartment extends Department {
    constructor() {
        super('Accounting and Auditing'); // 在派生类的构造函数中必须调用 super()
    }
    printMeeting(): void {
        console.log('The Accounting Department meets each Monday at 10am.');
    }
    generateReports(): void {
        console.log('Generating accounting reports...');
    }
}
let department: Department; // OK:允许创建一个对抽象类型的引用
department = new Department(); // Error: 不能创建一个抽象类的实例
department = new AccountingDepartment(); // OK:允许对一个抽象子类进行实例化和赋值
department.printName(); // OK
department.printMeeting(); // OK
department.generateReports(); // Error: 方法在声明的抽象类中不存在
复制代码

implements 类实现接口

interface Age {
  age: number;
}

interface Title{
  title: string;
}
class title implements Title, Age{
  title: string = '兔兔';
  age: number = 18;
}

T 泛型

// 泛型函数
function identity<T>(arg: T): T {
    return arg;
}
identity<number>(1); // OK:明确的指定`T`是`number`类型
identity(1); // OK:让编译器自己推断类型

// 泛型约束,在函数内部使用泛型变量的时候,由于事先不知道它是哪种类型,所以不能随意的操作它的属性或方法。
function loggingIdentity<T>(arg: T): T {
    console.log(arg.length); // Error
    return arg;
}
function loggingIdentity<T>(arg: T[]): T[] {
    console.log(arg.length);  // OK
    return arg;
}

// 泛型接口
interface Person<T> {
    name: T;
    getAge(arg: T): T;
}

let myIdentity: Person<string> = {
    name: "兔兔",
    getAge(name) {
        return name
    }
};

// 泛型类
class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;
}
let myGenericNumber = new GenericNumber<number>();

// 泛型参数的默认类型
function createArray<T = string>(length: number, value: T): Array<T> {
}

类型守卫

// 要定义一个类型守卫,我们只要简单地定义一个函数,它的返回值是一个类型谓词
function isA(person: A | B): person is A{
    return(person as A).name !== undefined;
}

// 使用
function doSomething(person: A | B): void {
    if(isA(person)) { // OK
        // ...
    }
}

// 使用in操作符
// typeof类型守卫
// instanceof类型守卫
// 
上一篇下一篇

猜你喜欢

热点阅读