More strong Promise<T> in lib.es6.d.ts
See original GitHub issueI think it would be good to be able to define the type of error in Promise
type. I suggest promise type to be something like this:
interface Promise<T,U> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: U) => TResult | PromiseLike<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: U) => void): Promise<TResult>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(onrejected?: (reason: U) => T | PromiseLike<T>): Promise<T>;
catch(onrejected?: (reason: U) => void): Promise<T>;
[Symbol.toStringTag]: string;
}
Or at least have a Promise<T, U>
type which extends Promise<T>
.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:11
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to use Typescript with native ES6 Promises
Call npm install --save @types/es6-promise and tsc --init . The first npm install command will change your package.
Read more >lib.d.ts - TypeScript Deep Dive - Gitbook
A special declaration file lib.d.ts ships with every installation of TypeScript. ... ambient declarations for more modern (es6) stuff like Promise .
Read more >TSConfig Option: lib - TypeScript
High Level libraries Additional APIs available in ES2015 (also known as ES6) - array. find , Promise , Proxy , Symbol , Map...
Read more >Typescript Typings: The Complete Guide: @types Compiler ...
Does Typescript type safety necessarily mean more ceremony when writing ... node_modules/@types/es6-promise/index.d.ts(42,19): error TS2300: ...
Read more >Promise - JavaScript - MDN Web Docs
The Promise object represents the eventual completion (or failure) of ... Promise proposal contains more details about promise terminology.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is another good use-case for implementing #2175.
I agree the ideal solution would be to have typed exceptions and then being able to correctly type Promises… but seeing that it may take a looooong time to have typed exceptions, I created a library that works likes promises but with a subtle difference that allows me to have typed errors and a pretty good error inference. Been using it on production for more than 6months and I’m really happy with the results.
The whole problem arises because we can’t forbid a function from throwing, and we cant know the type that it’s thrown, so what I did is favour the idiomatic way of returning an error, which is returning a rejected “Promise” and if for some reason a function throws, i put the error inside an UnknownError class, with an unknown property.
Here’s the library https://github.com/ts-task/task and here is a video explain in it with more detail https://www.youtube.com/watch?v=T7O1T1wmw00