无法对类型缺少调用或构造签名的表达式使用“ new” [英] Cannot use 'new' with an expression whose type lacks a call or construct signature

查看:496
本文介绍了无法对类型缺少调用或构造签名的表达式使用“ new”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取停止在console.log中的错误?

How do I get the error to stop in my console.log?


不能将'new'用于缺少类型的表达式

Cannot use 'new' with an expression whose type lacks a call or construct signature.



var audioContext = new window.AudioContext();

唯一的方法是这样做吗?

Is the the only way to do it?

    var AudioContext = <any>window.AudioContext || <any>window.webkitAudioContext;
    var audioContext = new AudioContext();


推荐答案

默认情况下,未定义此类全局API在内置的打字稿 Window 界面中。但是您可以通过声明合并在窗口上对其进行缩放,如下所示:

By default these kind of global APIs aren't defined in the built-in typescript Window interface. But you can decalre it on the window through declaration merging like so:

declare namespace window {
    const AudioContext:{
        new():AudioContext;
    }
}

您应该可以正常使用它,例如:

you should then be able to use it normally like:

var audioContext = new window.AudioContext();

这篇关于无法对类型缺少调用或构造签名的表达式使用“ new”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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