美文网首页
typescript中的class与interface的区别

typescript中的class与interface的区别

作者: theBigEye | 来源:发表于2018-08-22 17:19 被阅读0次

前言

typescript中声明一个类型,我们通常会有两种做法:
1.使用class

  export default class state {
    userInfo: {
        name: string,
        age: number
    }
}

2.使用interface

 export interface STATE {
    userInfo : {
        name: string,
        age: number
    }
}

那么这两种声明类型的方案有什么区别?

由于typescript的宗旨是兼容js,运行时要擦除所有类型信息,因此interface在运行时是会被完全消除的。而class经过编译后,在运行时依然存在。因此如果要声明的类型只是纯粹的类型信息,只需要声明interface即可。

相关文章

网友评论

      本文标题:typescript中的class与interface的区别

      本文链接:https://www.haomeiwen.com/subject/zyyeiftx.html