2

I'm trying to use this but the compiler is producing the following:

Client.ts(2,5): error TS2134: Subsequent variable declarations must have the same type. 
Variable 'XMLHttpRequest' must be of type '{ prototype: XMLHttpRequest; LOADING: number; 
DONE: number; UNSENT: number; OPENED: number; HEADERS_RECEIVED: number; new(): 
XMLHttpRequest; }', but here has type 'any'.

For reference the line producing the error is:

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

Am I right in assuming that this is because node.d.ts doesn't have a definition for this module? If so how would I go about implementing the definition, the bit that has me confused is the prototype and new() having type XMLHttpRequest. Is this a recursive reference or will say an empty class declaration suffice?

2 Answers 2

1

This is because XMLHttpRequest is defined in lib.d.ts :

declare var XMLHttpRequest: {
    prototype: XMLHttpRequest;
    new (): XMLHttpRequest;
    LOADING: number;
    DONE: number;
    UNSENT: number;
    OPENED: number;
    HEADERS_RECEIVED: number;
}

This conflicts with your definition of XMLHttpRequest which is actually resolved to any

var XMLHttpRequest

If this lib is compatible with the browser XMLHttpRequest you can do (splitting declaration and assignment). This way the compiler doesn't try to redefine it as any:

 var XMLHttpRequest;
 XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

If not I suggest you use a different name, OR compile with --noLib compiler flag.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the renaming worked. I wasn't aware definitions were being used from elsewhere, in hindsight it's obvious it would have to.
0

Yes node.d.ts wouldn't have that declaration.

I have 2 possible options for you create a node-XMLHttpRequest.d.ts manually. (I couldn't find one defined already) or use var XMLHttpRequest = <any>(require('xmlhttprequest').XMLHttpRequest);

If you are using this library a lot in your code I would recommend defining the node-XMLHttpRequest.d.ts file to get the benefit from typescript type checking.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.