ts 函数重载

class User {
  constructor(public readonly name: string, public readonly value: Function) {}
}

class Data {
  values: any[] = [];

  register(name: string, value: Function): void;
  register(user: User): void;
  register(name: string | User, value?: Function) {
    if (name instanceof User) {
      this.values.push(name);
    } else {
      this.values.push(new User(name, value!));
    }
  }

}

const data = new Data();

data.register("a", () => {});
data.register(new User('b', () => {}));

console.assert( data.values.length === 2 )
posted @ 2020-12-08 09:48  Ajanuw  阅读(810)  评论(0编辑  收藏  举报