typescript知识点

78 阅读1分钟

1.概述

TypeScript 是 JavaScript 的一个超集

2.类型

  • number
  • string
  • Array
  • any
  • null undefined
  • Enums
  • bigint
  • symbol
  • Functions
  • Object Types
  • Union Types |
  • Type Aliases
  • Interfaces
  • Type Assertions

泛型 约束 重载 先匹配最开始的

索引签名 继承 type &; interface extends; 断言 as 元组类型是另一种数组类型,它确切地知道它包含多少元素,以及它在特定位置包含哪些类型 模板文本类型 模块 keyof typeof 可迭代Iterable

其他类型

  • void 可返回任何类型
  • object
  • unknown
  • never
  • Function readonly

内置类型

  • Uppercase
  • Lowercase
  • Capitalize
  • UnCapitalize

class

  • implements 可选值不会实现
  • extends 继承
  • private 可以使用#代替 es2021后可以使用WeakMap做到真正的私有
  • abstract 抽象类

declare

  • namespace
  • 重载函数
  • 全局变量、函数

export module declare namespace declare class declare global

导入声明

  • 库依赖于全局库,使用///<reference types="…"/>
  • 一个模块import

模块解析 解析策略 tsc --traceResolution查看解析目录

  • node
    • 模仿Node.js的模块解析机制
  • Classic
    • 非相对模块导入 导入目录沿着目录树向上走
      // /root/src/a.js
      import { b } from "moduleB"
      
      1. /root/src/moduleB.ts

      2. /root/src/moduleB.d.ts

      3. /root/moduleB.ts

      4. /root/moduleB.d.ts

      5. /moduleB.ts

      6. /moduleB.ts