typescript TypeScript中的类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript TypeScript中的类型相关的知识,希望对你有一定的参考价值。

//  interfaces are named js objects
interface Name {
  firstName: string;
  lastName: string;
}

//  classes are also named js objects
class Person {
  public fullName: string;      //  have properties but with visibility
  //  have constructors as well
  constructor(private name: Name) {     //  scoped constructor parameters don't need to be declared explicitly
      this.name = name;
      this.fullName = this.name.firstName + " " + this.name.lastName;
  }

  //  have methods
  public introduce() {
    let introduction = 'Hi, my name is ' + this.name.firstName + '.';
    return introduction;
  }

}

//  classes can be extended
class Resident extends Person {
  public address;                               //  only new/overridden properties/methods need to be explicit
  constructor(name: Name, address: string) {
    super(name);                                //  parent constructor called with super
    this.address = address;
  }

  public introduce() {
    //  parent method called with super.method()
    let introduction = super.introduce() + ' And my address is: ' + this.address;
    return introduction;
  }
  
}

以上是关于typescript TypeScript中的类型的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript 中的基本数据类型

Typescript 入门手册之函数类型在 TypeScript 中的应用

Typescript 入门手册之函数类型在 TypeScript 中的应用

typescript TypeScript中的类型

typeScript中的数据类型

Typescript入门手册之基本类型在TypeScript中的应用