superagent/superagent.d.ts file in this repo and had problems.xxxx/xxxx.d.ts.After updating to latest superagent.d.ts, the commit 199acf46b3b6c8e047417df20e551719c5c4979d introduced a reference to Promise that is not found. This was removed from the supertest-as-promised.d.ts in the same commit and appears to cause an issue, but can't figure where the Promise definition is coming from.
Actual error from running typings install:
typings/globals/superagent/index.d.ts(78,31): error TS2304: Cannot find name 'Promise'.
please use it with es6-promise.d.ts or --target es6 or --lib es2015(tsc 2.0.0)
If it uses another type definition, then why isn't it referenced as an import? If you're building for ES5, this isn't clear at what the problem is since there are plenty of different promise library implementations out there & no clue as to which one is ideal for superagent
Same problem. I'm a total Typescript noob. Please be more specific as to what we need to do to fix.
To get it compiling, I did the following, but it feels very wrong.
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/247c4be0b7939a5558f9ff485f9a8abe6085b9d0/superagent/superagent.d.ts
/// <reference path="../../modules/es6-promise/index.d.ts" />
declare module "superagent" {
import stream = require('stream');
import {Promise} from 'es6-promise'
@dcworldwide - to get it working, so you don't have to maintain anything yourself, I'm just using the last version by including this in my typings.json and ignoring the out of date message:
"superagent": "registry:dt/superagent#1.4.0+20160724070541",
It's not the best approach, but the type def should reference the things it requires within it... not just assume people can figure it out.
@andrewconnell Thanks Andrew. By chance do you know how your workaround would apply to TS 2.x? I.e. using @types instead of typings.
I don't... not sure what @types is ... can you share a link? Looking at the TS 2.0 roadmap I don't see a mention if it.
I'm using Bluebird as a Promise poly-fill and getting this error:
node_modules/@types/superagent/index.d.ts(83,15): error TS2430: Interface 'Request' incorrectly extends interface 'Promise<Response>'.
Types of property 'timeout' are incompatible.
Type '(ms: number) => this' is not assignable to type '(ms: number, message?: string | Error | undefined) => Bluebird<Response>'.
Type 'this' is not assignable to type 'Bluebird<Response>'.
Type 'Request' is not assignable to type 'Bluebird<Response>'.
Types of property 'get' are incompatible.
Type '(field: string) => string' is not assignable to type '<U>(key: string | number) => Bluebird<U>'.
Type 'string' is not assignable to type 'Bluebird<any>'.
I am guessing that using an explicit import in the superagent d.ts would solve it...
@andrewconnell I think what @dcworldwide means is how to get it working when not using the Typescript Definition Manager, i.e. only managing type definitions with npm @types/package-name packages.
The solution which worked for me is installing the @types/es6-promises package with npm and adding es6-promises to the compilerOptions.types array in tsconfig.json.
You have to make sure to not use any other promise libraries' type definitions, though. I used ts-promise before and got hard to understand compiler errors when trying to use the Promise type from both like "Type 'Promise<number>' is not assignable to type 'Promise<any>'." This is obvious in context, but might bite you when it happends when you are not aware of the connection.
The suggestion given by @vvakame worked for me, at least to the point where the file would compile successfully:
tsc --target es6 myfile.ts
DO NOT change your target to es6, unless you are certain that your target environment supports it! Most current web browsers do not. The solution given by @SKoschnicke is a better one for web targets, because you maintain your es5 target output.
Tested and solved the problem for me.
As @BeheadedKamikaze noted, do not switch your target unless you know you are always running on an ES6 engine (e.g. node V6./V7.). For web, it is likely that some browsers your uses will use are not running ES6 engines.
use --lib es2016.promise (or --lib es2015.promise, dom for web) to get the Promise definitions library included in your compilation. Also note that this does not make a Promise implementation available at runtime, you are still required to include a pollyfill if Promise is not supported on the engine you are running against (e.g. IE10/IE11).
Most helpful comment
I'm using Bluebird as a Promise poly-fill and getting this error:
I am guessing that using an explicit import in the superagent
d.tswould solve it...