Cannot use new with expression typescript

The problem is that the compiler treats the contextClass variable as an instance of AudioContext while in fact it’s merely a constructor function for an AudioContext.

You can simply use any and the compiler will let you new it:

let contextClass : any = window.AudioContext || window.webkitAudioContext;
let context: AudioContext = new contextClass();

Another option would be to introduce a Constructable<T> interface that will let you new up those types:

interface Constructable<T> {
    new(...args: any) : T;
}

interface Window
{
    AudioContext: Constructable<AudioContext>;
    webkitAudioContext: Constructable<AudioContext>
}

let contextClass = window.AudioContext || window.webkitAudioContext;
let context: AudioContext = new contextClass();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)