Typescript and javascript class

Typescript and javascript class

本文关键字:class javascript and Typescript      更新时间:2023-09-26

如何在类型脚本中安装javascript类?

例如:

class LiveAudioFreq {
    context;
    constructor(){
        context = new window.AudioContext();
    }
}

告诉我错误:

错误TS2094:"Window"类型的值上不存在属性"AudioContext"。

并且拒绝编译,如何在ts文件中使用javascript方法?

在构造函数中,使用:

this.context = ...

而不仅仅是

context = ...

此外,您在哪里定义了AudioContext?这很可能在标准库中不会出现,直到它在所有地方实现,所以您可能需要创建一个环境声明。