-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Provide a way to load Bluebird globally in es6 compilation target. #12382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
First of all do not add the line export as namespace Promise; Add this line instead export as namespace Bluebird; Then add a file called interface Promise<T> extends Bluebird<T> {}
interface PromiseConstructor {
delay: typeof Bluebird.prototype.delay;
} Add the following to your {
"compilerOptions": {
"lib": [
"es5",
"dom"
]
}
} That is all you should need to do. |
Hi Aluan, Thanks for your answer. It led me to the right direction. Your solution works for es6, however there are some edge cases, when it doesn't. Also, I'd like devs to just I'm trying to prepare a bluebird-global.d.ts, that would do, what you advised me to do, for all devs. I got it working, but could you please take a look at it and like "sign it off" maybe? You seem to be up to date with current best practices for writing declaration files, so I'd like to hear your opinion on this. Just tell me whether you see anything wrong with it, please. Steps:
Differences and implementation details:
What do you think about it? |
First of all I apologize for any confusion due to my example which was actually kind of a hack. Part of the problem with This is made harder by the fact that I'm not sure if this is relevant, but after having some difficulty over the past few months using the declaration files for core-js, I have transitioned to generally just using the I will review what you have in greater detail as soon as I get some time. |
Yeah, I noticed, that some of Bluebird's methods are slightly different than the standard's. Will see how it goes. I haven't thought of using the And thanks for taking time to look at my solution. Just wanted to mention, that I'd just like you to take a look at the Thanks. |
Update: The code is now in DefinitelyTyped repo (link). I'm pretty happy with it, so there's no need to check anything anymore. I'm removing the proof-of-concept git repo now and closing this ticket. Thanks for all the tips 👍 |
@d-ph , should we add bluebird-global to the types only if we have already declared a types config option? Otherwise, doing so kills all our other types. |
Sorry, but I don't understand the problem you're describing. Could you give me a steps-to-reproduce, so I can better understand the problem you're facing, please?
I recommend adding If, however, you'd rather not put
If you decide not to put Thanks. |
Thanks for your long response, @d-ph ! [Read the following, but also read my realization after the "--------" please :) ] According to https://www.typescriptlang.org/docs/handbook/tsconfig-json.html , "If types is specified, only packages listed will be included." I am interpreting this to mean that if our project currently does not have a types config declaration, then if we just add bluebird-global, all other type lookups will fail. Indeed, when I add
to my tsconfig, I immediately get compilation errors. But now I'm realizing that all that meant was that I needed to add jQuery as a type as well, while all of my other type lookups are explicit with import statements. So is my understanding correct that for types that need to be globally available without an import, I put them in the types option in the tsconfig, but for types that I import, I can just continue using the normal import? |
Also, another thing: I've followed your instructions, but it doesn't seem like bluebird is getting included in my bundle. So I've done Even if I do Any ideas? What am I doing wrong? |
If I do the following, finally bluebird replaces the native promise:
But I have a feeling this isn't the right approach... |
Re: Why
|
TypeScript Version: 2.1.1
Code
git clone -b e5-and-core-js-case https://github.com./d-ph/typescript-bluebird-as-global-promise.git
orgit clone -b vanilla-es6-case https://github.com./d-ph/typescript-bluebird-as-global-promise.git
cd typescript-bluebird-as-global-promise
npm install
node_modules/@types/bluebird/index.d.ts
by addingexport as namespace Promise;
above theexport = Bluebird;
line. (please ignore the fact that you're hacking 3rd code for a second)npm run tsc
Expected behavior:
Compilation succeeds.
Promise
is detected to be declared by bluebird.d.ts.Actual behavior:
Tsc says, that
lastly
,delay
andfinally
don't exist on typePromise
.More info:
Bluebird polyfills the global
Promise
, but Typescript ignores that (i.e. ignores theexport as namespace Promise;
line) whenPromise
is already defined by either thelib.es2015.promise.d.ts
es6 declaration file or thecore-js
declaration file.I think the problem doesn't lie in Typescript's ignoring the Bluebird's
export as namespace Promise;
, but in some sort of precedence order, when global symbols are being declared by .d.ts files. What I'd like to be able to do is to somehow tell Typescript, that Bluebird is my effectivePromise
provider. Maybe this could be achieved by making Typescript respect the order of@types
listed in tsconfig.json'scompilerOptions.types
array? I.e. the last item in that array wins any global namespace conflict?This problem most likely affects any polyfilling library in the wild, where the polyfill is.. more convenient to use than the standard solution, so devs decide to use it even after starting using newer js standard.
Btw. this ticket is sort of blocked by #10178 at the moment.
Thanks.
The text was updated successfully, but these errors were encountered: