0

For reasons beyond my control I'm trying to compile Typescript into "JScript" (~es5/IE8 JS) so that IIS/ASP Classic can evaluate it. It's sort of working via Babel + Rollup. One thing I'm stuck on is making Classic ASP's "Response" object accessible in typescript. I can't seem to overwrite the existing Response definition.

I thought something like this would work:

declare global {
    interface Response {
        write: (val: string) => void
    }
}

function writeSomething() {
    Response.write("foo")
}

I receive this error:

Property 'write' does not exist on type '{ new (body?: BodyInit, init?: ResponseInit): Response; prototype: Response; error(): Response; redirect(url: string | URL, status?: number): Response; }'.ts(2339)

The Response object looks like:

var Response: {
    new (body?: BodyInit, init?: ResponseInit): Response;
    prototype: Response;
    error(): Response;
    redirect(url: string | URL, status?: number): Response;
}
// This Fetch API interface represents the response to a request.
Sean
  • 509
  • 2
  • 11
  • Overwriting the definition is hard. Could you rename it to something else, then put `const MyNewThing = Response;` at the top of your script (assert or ignore if needed)? – kelsny Aug 31 '22 at 20:09
  • Wouldn't I be in the same boat with MyNewThing? TS won't let me convert one to the other because the shapes are different, ex: const MyNewThing = Response as NewResponse – Sean Aug 31 '22 at 20:12
  • 1
    You can define a new type for your new alias, and `//@ts-ignore` the line where you create the alias. – kelsny Aug 31 '22 at 20:13
  • Rollup's adding the @ts-ignore directly to the output, that's too bad. – Sean Aug 31 '22 at 20:25
  • 1
    Well you can also use `const MyNewThing = Response as unknown as NewResponse;` – kelsny Aug 31 '22 at 20:29
  • Your question is similar to what is asked [here](https://stackoverflow.com/questions/72742410/extending-global-var-in-typescript). There doesn't seem to be any solutions...yet – smac89 Sep 01 '22 at 05:53
  • Thanks @kelly and @smac89 it felt cleaner importing the `Response` object instead of using a globally defined one. That let me abstract all the interface and overwrite magic into a separate file. The link from the other post had a more elegant solution using `assert` but kelly's `as unknown as NewResponse` was easier so I opted for that. – Sean Sep 06 '22 at 18:55

0 Answers0