TypeScript 基础
📝

TypeScript 基础

Created by
ZhaohaoZhaohao
Tags
Frontend
Published
Published 2024-08-09 00:00
Last edited time
Last updated 2024-08-09 07:58

基础

  1. 类型推断
    1. Copy typescript
      let a = 'abc' a = 'def' // a = 1 会报错
  1. 类型注解
    1. Copy typescript
      let a: string
  1. 类型断言
    1. Copy typescript
      let numArr = [1, 2, 3] const result = numArr.find(item => item > 2) as number // 操作的最终结果,断言是 number 类型 result * 5
  1. 基础类型和联合类型
    1. Copy typescript
      //基础类型 let v1: string = 'abc' let v2: number = 10 let v3: boolean = true let nu: null = null let un: undefined = undefined //联合类型 let v4: string | null = null let v5: 1 | 2 | 3 = 2
  1. 数组、元组、枚举
    1. Copy typescript
      //数组 let arr: number[] = [1, 2, 3] let arr1: Array<string> = ['a', 'b', 'c'] //元组 let t1: [number, string, number] = [1, 'a', 2] let t1: [number, string, number?] = [1, 'a'] // ?表示可有可无 //枚举 enum MyEnum {A, B, C} // 默认从0开始分配 console.log(MyEnum.A) console.log(MyEmum[0])//这俩访问的是同一个 // void 默认是 undefined
  1. 函数
    1. Copy typescript
      function MyFn(a: number, b: string, c?: boolean, ...rest: number[]): number {// 可选参数要在最后 return 100; } const f = MyFn(20, 'abc', true, 1, 2, 3, 4, 5);
  1. 接口
    1. Copy typescript
      interface Obj { name: string; age: number; } const obj: Obj = { name: 'a', age: 10, };
  1. 类型别名
    1. Copy typescript
      type MyUserName = string | number; let a: MyUserName = 10; let b: MyUserName = 'Luke';
  1. 泛型
    1. Copy typescript
      function myFn<T>(a: T, b: T): T[] { return [a, b]; } myFn<number>(1, 2); myFn<string>('a', 'b');
 

进阶

  1. 函数重载
    1. Copy typescript
      function hello(name: string): string; function hello(age: number): string; function hello(value: string | number): string { if (typeof value === "string") { return "你好,我的名字是" + value; } else if (typeof value === "number") { return "你好,我的年龄是${value}"; } else { return "非法格式"; } } hello("abc"); hello(18);
  1. 接口继承
    1. Copy typescript
      interface Parent { prop1: string; prop2: number; } interface Child extends Parent { prop3: boolean; } const myObj: Child = { prop1: 'a', prop2: 1, prop3: true, };
  1. 类的修饰符
    1. Copy typescript
      class Article { public title: string; // 不写就是 public content: string; aaa?: string; bbb = 100; private ccc?: string; protected ddd?: string; static author: string; // static 要写在最后 private static readonly: string = "Luke"; // 只读且 private constructor(title: string, content: string) { this.title = title; this.content = content; } } const a = new Article("标题", "内容"); // a.ccc 报错 a.title; // a.ddd 报错 Article.author; class B extends Article { constructor(title: string, content: string) { super(title, content); this.ddd; } }
  1. 存取器
    1. Copy typescript
      class User { private _password: string = ""; get password(): string { return "******"; } set password(newPass: string) { this._password = newPass; } } const u = new User(); console.log(u.password);
  1. 抽象类
    1. Copy typescript
      没啥用
  1. 类实现接口
    1. Copy typescript
      没啥用
  1. 泛型类
    1. Copy typescript
      没啥用

Comments