question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

More strong Promise<T> in lib.es6.d.ts

See original GitHub issue

I 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:open
  • Created 8 years ago
  • Reactions:11
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
NoelAbrahamscommented, Oct 27, 2015

This is another good use-case for implementing #2175.

interface Promise<T, U = Error> {
 //  ...
}

2reactions
hrajchertcommented, Nov 7, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found