How do I represent a "standard" Javascript object constructor (new ()) in TypeScript when I can't modify the original Javascript? -
i'm working on project add type annotations (a .d.ts file) large existing javascript code base. .d.ts file live alongside javascript, can't make changes javascript support project.
in existing code, there's this:
...
window.myglobal.service = function () { } window.myglobal.service.prototype = { baseurl: 'http://...'; }
...
with expectation we'll later
var svc = new window.myglobal.service();
i can't figure out how represent in typescript. have
interface service { baseurl: string; }
but i'm not sure put in interface myglobal. i've tried
interface myglobal { service: service; }
but can't use new() on that. can change
interface myglobal { service(): void; }
and let me use new(); loses type information service interface.
is possible handle situation in typescript?
option 1:
declare module myglobal { class service { baseurl: string; } } interface window { myglobal: { service: myglobal.service; } } var svc = new window.myglobal.service(); var x = svc.baseurl;
option 2:
declare module myglobal { interface service { baseurl: string; } } interface window { myglobal: { service: { new(): myglobal.service } } } var svc = new window.myglobal.service(); var x = svc.baseurl;
Comments
Post a Comment