Matthew Harwood Matthew Harwood - 27 days ago 4
TypeScript Question

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

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

Answer

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