Cannot use 'new' with an expression whose

2019-08-05 04:22发布

问题:

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

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();

回答1:

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();