typescript class可以作为类型定义被扩展

// 定义一个class类
class Poin2 {
    
    
  x: number;
  y: number;
  constructor(x: number, y: number) {
    
    
    this.x = x;
    this.y = y;
  }
}

// 作为类型使用
const test1:Poin2 = {
    
    
  x: 1,
  y: 2
}

// 被接口扩展
interface Point3d extends Poin2 {
    
    
  z: number;
}

const btn: Point3d = {
    
    
  x: 1,
  y: 2,
  z: 3,
};

// 被类型别名扩展
type fhsadjk = {
    
    
  z: string;
} & Poin2;

const btn1: fhsadjk = {
    
    
  z: '123',
  x:1,
  y: 2
}

猜你喜欢

转载自blog.csdn.net/glorydx/article/details/112625642