typescript 抽象类,莫名报错

查看:150
本文介绍了typescript 抽象类,莫名报错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

abstract class Department{
    constructor(public name:string){
    }
    printName():void{
        console.log(`the department is ${this.name}`);
    }
    abstract printMeetring():void
}

class Account extends Department{
//**Class 'Account' incorrectly extends base class 'Department'.  Property 'name' is optional in type 'Account' but required in type 'Department'. 显示这个错误**
    constructor(name:string){
        super(name);
    }
    printMeetring():void{
        console.log(`${this.name} you have a meetring`);
    }
}

如果此时把 class Account extends Department 换为 class account extends Department有没有错误了,难道typescript 无法识别Account?????

解决方案

谢邀
问题原因嘛...因为Account这个interface确实存在,你的class名字与它起冲突了

这个问题的坑在于Web Authentication这个尚未发布的草案上,而ts又过于超前的去支持了这个草案...

但目前的浏览器没有一个完整支持的,而且还在草案阶段,api很可能之后也会改,ts把它直接放到dom api里的行为确实有点不太妥

目前似乎没有好的没有解决方案,换个名字吧(顺便兼容未来的浏览器...万一变正式标准了呢=.=)

参考:
ts对Account的定义
Web Authentication草案

这篇关于typescript 抽象类,莫名报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆