modify es6 promise typings by adding exceptions to PromiseLike





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















Why? To find errors in a large code base, where ng.IPromise and Promise are mixed together.



I have already done that for ng.IPromise typings by modifying this:



interface IPromise<T> {
then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): IPromise<TResult>;
// ...
}


to this:



interface IPromise<T> {
then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): TResult extends Promise<any> ? never : IPromise<TResult>;
// ...
}


But native Promise typings are more complex, cause it introduces a PromiseLike type to resolve result of any object which has a then method:



interface PromiseLike<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
}

interface Promise<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
}


And I want to achieve a behavior, where every PromiseLike except an ng.IPromise resolves.



Have tried to simplify the typings to better understand them. So basically we can simplify the problem to smth like this - how to modify the following typings:



interface Bar<T> {
then<K>(f: (value: T) => K | Bar<K>): Bar<K>;
}

interface PartialFoo<T> {
then<K>(f: (value: T) => K | PartialFoo<K>): PartialFoo<K>;
}

interface Foo<T> {
then<K>(f: (value: T) => K | PartialFoo<K>): Foo<K>;
}


so that:



let a: Foo<string>;
let b: PartialFoo<number>;
let c: Bar<number>;

let x: Foo<string> = a.then(() => a); // is ok
let y: Foo<number> = a.then(() => b); // is ok
let z: never = a.then(() => c); // returns type never, so chaining anything else will give an error


Also another thing, where I don't really understand how Typescript works, why when modifying interfaces in this way:



interface PartialFoo<T> {
then<K>(f: () => K | PartialFoo<K>): PartialFoo<K>;
}

interface Foo<T> {
then<K>(f: () => K | PartialFoo<K>): Foo<K>;
}

let a: Foo<string>;
let x = a.then(() => a);


type for x is Foo<Foo<string>>, but in case of:



interface Foo<T> {
then<K>(f: () => K | Foo<K>): Foo<K>;
}


it's still resolved to Foo<string>. What role value: T is playing, if it's not even used?










share|improve this question





























    7















    Why? To find errors in a large code base, where ng.IPromise and Promise are mixed together.



    I have already done that for ng.IPromise typings by modifying this:



    interface IPromise<T> {
    then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): IPromise<TResult>;
    // ...
    }


    to this:



    interface IPromise<T> {
    then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): TResult extends Promise<any> ? never : IPromise<TResult>;
    // ...
    }


    But native Promise typings are more complex, cause it introduces a PromiseLike type to resolve result of any object which has a then method:



    interface PromiseLike<T> {
    then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
    }

    interface Promise<T> {
    then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
    }


    And I want to achieve a behavior, where every PromiseLike except an ng.IPromise resolves.



    Have tried to simplify the typings to better understand them. So basically we can simplify the problem to smth like this - how to modify the following typings:



    interface Bar<T> {
    then<K>(f: (value: T) => K | Bar<K>): Bar<K>;
    }

    interface PartialFoo<T> {
    then<K>(f: (value: T) => K | PartialFoo<K>): PartialFoo<K>;
    }

    interface Foo<T> {
    then<K>(f: (value: T) => K | PartialFoo<K>): Foo<K>;
    }


    so that:



    let a: Foo<string>;
    let b: PartialFoo<number>;
    let c: Bar<number>;

    let x: Foo<string> = a.then(() => a); // is ok
    let y: Foo<number> = a.then(() => b); // is ok
    let z: never = a.then(() => c); // returns type never, so chaining anything else will give an error


    Also another thing, where I don't really understand how Typescript works, why when modifying interfaces in this way:



    interface PartialFoo<T> {
    then<K>(f: () => K | PartialFoo<K>): PartialFoo<K>;
    }

    interface Foo<T> {
    then<K>(f: () => K | PartialFoo<K>): Foo<K>;
    }

    let a: Foo<string>;
    let x = a.then(() => a);


    type for x is Foo<Foo<string>>, but in case of:



    interface Foo<T> {
    then<K>(f: () => K | Foo<K>): Foo<K>;
    }


    it's still resolved to Foo<string>. What role value: T is playing, if it's not even used?










    share|improve this question

























      7












      7








      7


      1






      Why? To find errors in a large code base, where ng.IPromise and Promise are mixed together.



      I have already done that for ng.IPromise typings by modifying this:



      interface IPromise<T> {
      then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): IPromise<TResult>;
      // ...
      }


      to this:



      interface IPromise<T> {
      then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): TResult extends Promise<any> ? never : IPromise<TResult>;
      // ...
      }


      But native Promise typings are more complex, cause it introduces a PromiseLike type to resolve result of any object which has a then method:



      interface PromiseLike<T> {
      then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
      }

      interface Promise<T> {
      then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
      }


      And I want to achieve a behavior, where every PromiseLike except an ng.IPromise resolves.



      Have tried to simplify the typings to better understand them. So basically we can simplify the problem to smth like this - how to modify the following typings:



      interface Bar<T> {
      then<K>(f: (value: T) => K | Bar<K>): Bar<K>;
      }

      interface PartialFoo<T> {
      then<K>(f: (value: T) => K | PartialFoo<K>): PartialFoo<K>;
      }

      interface Foo<T> {
      then<K>(f: (value: T) => K | PartialFoo<K>): Foo<K>;
      }


      so that:



      let a: Foo<string>;
      let b: PartialFoo<number>;
      let c: Bar<number>;

      let x: Foo<string> = a.then(() => a); // is ok
      let y: Foo<number> = a.then(() => b); // is ok
      let z: never = a.then(() => c); // returns type never, so chaining anything else will give an error


      Also another thing, where I don't really understand how Typescript works, why when modifying interfaces in this way:



      interface PartialFoo<T> {
      then<K>(f: () => K | PartialFoo<K>): PartialFoo<K>;
      }

      interface Foo<T> {
      then<K>(f: () => K | PartialFoo<K>): Foo<K>;
      }

      let a: Foo<string>;
      let x = a.then(() => a);


      type for x is Foo<Foo<string>>, but in case of:



      interface Foo<T> {
      then<K>(f: () => K | Foo<K>): Foo<K>;
      }


      it's still resolved to Foo<string>. What role value: T is playing, if it's not even used?










      share|improve this question














      Why? To find errors in a large code base, where ng.IPromise and Promise are mixed together.



      I have already done that for ng.IPromise typings by modifying this:



      interface IPromise<T> {
      then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): IPromise<TResult>;
      // ...
      }


      to this:



      interface IPromise<T> {
      then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: null | undefined, notifyCallback?: (state: any) => any): TResult extends Promise<any> ? never : IPromise<TResult>;
      // ...
      }


      But native Promise typings are more complex, cause it introduces a PromiseLike type to resolve result of any object which has a then method:



      interface PromiseLike<T> {
      then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
      }

      interface Promise<T> {
      then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
      }


      And I want to achieve a behavior, where every PromiseLike except an ng.IPromise resolves.



      Have tried to simplify the typings to better understand them. So basically we can simplify the problem to smth like this - how to modify the following typings:



      interface Bar<T> {
      then<K>(f: (value: T) => K | Bar<K>): Bar<K>;
      }

      interface PartialFoo<T> {
      then<K>(f: (value: T) => K | PartialFoo<K>): PartialFoo<K>;
      }

      interface Foo<T> {
      then<K>(f: (value: T) => K | PartialFoo<K>): Foo<K>;
      }


      so that:



      let a: Foo<string>;
      let b: PartialFoo<number>;
      let c: Bar<number>;

      let x: Foo<string> = a.then(() => a); // is ok
      let y: Foo<number> = a.then(() => b); // is ok
      let z: never = a.then(() => c); // returns type never, so chaining anything else will give an error


      Also another thing, where I don't really understand how Typescript works, why when modifying interfaces in this way:



      interface PartialFoo<T> {
      then<K>(f: () => K | PartialFoo<K>): PartialFoo<K>;
      }

      interface Foo<T> {
      then<K>(f: () => K | PartialFoo<K>): Foo<K>;
      }

      let a: Foo<string>;
      let x = a.then(() => a);


      type for x is Foo<Foo<string>>, but in case of:



      interface Foo<T> {
      then<K>(f: () => K | Foo<K>): Foo<K>;
      }


      it's still resolved to Foo<string>. What role value: T is playing, if it's not even used?







      typescript es6-promise






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 17:34









      Eduard GhazanchyanEduard Ghazanchyan

      53118




      53118
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486293%2fmodify-es6-promise-typings-by-adding-exceptions-to-promiselike%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486293%2fmodify-es6-promise-typings-by-adding-exceptions-to-promiselike%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen