Skip to content
本页目录

关键字与修饰符

系统性的罗列一下TypeScript中常用的关键字与修饰符。

类相关

类相关的关键字有:classextendssuperabstractstaticreadonlyoverride

class关键字

用于定义类。

TypeScript
class Kon {
  name: string
  constructor(name: string) {
    this.name = name
  }
}

extends关键字

用于类的继承。

TypeScript
class Yui extends Kon {
  sing() {
    console.log('平泽唯')
  }
}

super关键字

用于调用父类的构造函数或方法。

TypeScript
class Yui extends Kon {
  constructor(name: string) {
    super(name) // 调用父类的构造函数
  }
  sing() {
    console.log('平泽唯')
  }
}

abstract关键字

用于定义抽象类或抽象方法。

特别说明

  • 抽象类不能直接实例化;
  • 抽象方法必须在子类中实现。
TypeScript
abstract class Kon {
  abstract sing(): void
}

class Yui extends Kon {
  sing() {
    console.log('平泽唯')
  }
}

override关键字

用于显示标记子类中的方法或属性是重写父类的。

特别说明

  • 只能标记父类(包括抽象类)中已存在的属性或方法;
  • 若子类实现了接口中的方法,不能使用override标记,因为接口不是类
TypeScript
class Kon {
  sing() {
    console.log('轻音少女')
  }
}

class Yui extends Kon {
  override sing() { // 显示标记重写sing方法
    console.log('平泽唯')
  }
}

const yui = new Yui()
yui.sing() // 平泽唯
TypeScript
abstract class Kon {
  abstract sing(): void
}

class Yui extends Kon {
  override sing() { // 显示标记重写sing方法
    console.log('平泽唯')
  }
}

const yui = new Yui()
yui.sing() // 平泽唯
TypeScript
interface Kon {
  sing(): void
}

class Yui implements Kon {
  override sing() { // 报错:不能使用 override  
  sing() { // 正常写法
    console.log('平泽唯')
  }
}

const yui = new Yui()
yui.sing() // 平泽唯

static关键字

用于定义类的静态成员(属性或方法)。

特别说明

  • 静态成员属于类本身,而不是其实例。
TypeScript
class Mio extends Kon {
  static weight = '55kg'
  static sing() {
    console.log('秋山澪')
  }
}

readonly关键字

用于定义只读属性。

TypeScript
class Kon {
  readonly name: string
  constructor(name: string) {
    this.name = name
  }
}

const kon = new Kon('轻音少女')
kon.name = '孤独摇滚' // 报错:Cannot assign to 'name' because it is a read-only property. 

访问修饰符

类相关的修饰符有:publicprivateprotected

public修饰符

默认访问修饰符,成员可以在任何地方访问。

TypeScript
class Kon {
  public name: string
  constructor(name: string) {
    this.name = name
  }
}

private修饰符

成员只能在类的内部访问。

特别说明

  • 子类和实例中均不能访问。
TypeScript
class Kon {
  private mioWeight: string
  constructor(mioWeight: string) {
    this.mioWeight = mioWeight
  }

  getWeight() {
    console.log(this.mioWeight)
  }
}

const kon = new Kon('55kg')
console.log(kon.mioWeight) // 报错:Property 'name' does not exist on type 'Kon'.  
kon.getWeight() // 55kg

protected修饰符

成员可以在类内部和子类中访问。

特别说明

  • 实例(包括子类实例)中依然不能访问。
TypeScript
class Kon {
  protected mioWeight: string
  constructor(mioWeight: string) {
    this.mioWeight = mioWeight
  }
}

class Mio extends Kon {
  constructor(mioWeight: string) {
    super(mioWeight)
  }
  getWeight() {
    console.log(this.mioWeight)
  }
}

const mio = new Mio('55kg')
console.log(mio.mioWeight) // 报错:Property 'mioWeight' is protected and only accessible within class 'Kon' and its subclasses.   
mio.getWeight() // 55kg

类型相关

类型相关的关键字有:typeinterfaceenumkeyoftypeof

type关键字

用于定义类型别名。

TypeScript
type Members = 'yui' | 'mio'

interface关键字

用于定义接口。

特别说明

  • type的区别:
    • interface通过extends扩展,type通过交叉类型&扩展;
    • interface支持声明合并,type不支持。
TypeScript
interface Kon {
  sing(): void
}

implements关键字

用于类实现接口。

特别说明

  • 接口只定义结构,不包含实现,具体实现由类完成;
  • 类可以实现多个接口,用逗号分隔多个接口名;
  • 接口可以继承。
TypeScript
class Yui implements Kon {
  sing() {
    console.log('平泽唯')
  }
}

enum关键字

用于定义枚举类型。

特别说明

  • 由于字符串值不能保证唯一性,故反向映射只支持数字枚举。
TypeScript
enum Members {
  YUI,
  MIO
}

keyof关键字

用于获取对象类型的键的联合类型。

TypeScript
type Mio = {
  age: number
  weight: string
}

type Keys = keyof Mio // 'age' | 'weight'

typeof关键字

用于获取变量或值的类型。

TypeScript
const MEMBER = {
  name: 'mio',
  weight: '55kg'
}

type Mio = typeof MEMBER // { name: string; weight: string;}

函数相关

函数相关的关键字有:functionreturnthisvoid

function关键字

用于定义函数。

TypeScript
function sing() {
  console.log('轻音少女')
}

return关键字

用于从函数中返回值。

TypeScript
function sing() {
  return '轻音少女'
}

this关键字

用于引用当前对象的上下文。

TypeScript
class Kon {
  name: string
  constructor(name: string) {
    this.name = name
  }
  sing() {
    console.log(this.name)
  }
}

void关键字

用于表示函数没有返回值。

TypeScript
type VoidFn = () => void

变量相关

变量相关的关键字有:varletconstdeclare

var关键字

用于声明函数作用域的变量(不推荐使用)。

TypeScript
var yui = '平泽唯'

let关键字

用于声明块级作用域的变量。

TypeScript
let mio = '秋山澪'

const关键字

用于声明常量,值不能重新赋值。

TypeScript
const KON = '轻音少女'

declare关键字

用于声明变量、函数、类、模块或命名空间的存在,而无需提供具体的实现。

特别说明

  • varletconst的区别:
    • declare仅用于声明类型,不分配内存或定义实现;
    • varletconst用于定义变量并分配内存。
  • typeinterface的区别:
    • declare用于声明变量、函数、类、模块等的类型信息;
    • typeinterface用于定义类型别名或对象类型。
  • .d.ts文件的关系:
    • .d.ts文件通常用于描述JavaScript库或模块的类型信息;
    • .d.ts文件中,declare是默认的,因此可以省略。
TypeScript
declare const YUI: string
TypeScript
declare function sing(): void
TypeScript
declare class Kon {
  constructor(name: string)
  sing(): void
}
TypeScript
declare module 'kon' {
  export function sing(): void
}
TypeScript
declare namespace Kon {
  function sing(): void
}
TypeScript
// 为已有的类添加静态方法
class Kon {
  sing() { }
}
declare namespace Kon {
  function name(): void
}

Kon.name() // 调用静态方法
TypeScript
// 扩展 kon 模块
declare module 'kon' {
  interface Member {
    yui: { age: number }
    mio: { weight: string }
  }
}

其它关键字

其它关键字:asinnewinstanceofinfer

as关键字

用于类型断言。

TypeScript
const YUI: any = '平泽唯'
const len = (YUI as string).length

in关键字

用于检查对象中是否存在某个属性。

TypeScript
const MEMBER = {
  name: 'mio',
  weight: '55kg'
}

if ('weight' in MEMBER) {
  // true
}

new关键字

用于创建类的实例。

TypeScript
class Kon { }
const kon = new Kon()

instanceof关键字

用于检查对象是否是某个类的实例。

TypeScript
kon instanceof Kon // true

infer关键字

用于在条件类型中声明一个待推断的类型变量。

特别说明

  • infer只能在条件类型的extends子句中使用。
TypeScript
type Kon<T> = T extends infer U ? U : never

const MIO = {
  name: '秋山澪',
  age: 16,
  weight: '55kg'
}

type Member = Kon<typeof MIO>

// type Member = {
//   name: string
//   age: number
//   weight: string
// }