Typescript Generic Promise Return Type












1















Basically I'm trying to implement a function that always returns a fulfilled promise of the same "type" i pass to the function as a parameter



So if I call with a boolean it returns a fulfilled Promise, if I call with a string parameter it returns a fulfilled Promise and so on..



what I tried so far:



const PromiseOK = <T>(val: T): Promise<T> => {
return Promise.resolve(val);
};


I don't know if it's the proper way to do it and in any case it breaks if i try to get a Promise< void >



Any suggestion will be appreciated










share|improve this question



























    1















    Basically I'm trying to implement a function that always returns a fulfilled promise of the same "type" i pass to the function as a parameter



    So if I call with a boolean it returns a fulfilled Promise, if I call with a string parameter it returns a fulfilled Promise and so on..



    what I tried so far:



    const PromiseOK = <T>(val: T): Promise<T> => {
    return Promise.resolve(val);
    };


    I don't know if it's the proper way to do it and in any case it breaks if i try to get a Promise< void >



    Any suggestion will be appreciated










    share|improve this question

























      1












      1








      1


      1






      Basically I'm trying to implement a function that always returns a fulfilled promise of the same "type" i pass to the function as a parameter



      So if I call with a boolean it returns a fulfilled Promise, if I call with a string parameter it returns a fulfilled Promise and so on..



      what I tried so far:



      const PromiseOK = <T>(val: T): Promise<T> => {
      return Promise.resolve(val);
      };


      I don't know if it's the proper way to do it and in any case it breaks if i try to get a Promise< void >



      Any suggestion will be appreciated










      share|improve this question














      Basically I'm trying to implement a function that always returns a fulfilled promise of the same "type" i pass to the function as a parameter



      So if I call with a boolean it returns a fulfilled Promise, if I call with a string parameter it returns a fulfilled Promise and so on..



      what I tried so far:



      const PromiseOK = <T>(val: T): Promise<T> => {
      return Promise.resolve(val);
      };


      I don't know if it's the proper way to do it and in any case it breaks if i try to get a Promise< void >



      Any suggestion will be appreciated







      typescript generics promise typescript-generics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 11:00









      PlasticPlastic

      1,94521632




      1,94521632
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You implementation seems fine, the problem with void is that the parameter is still expected. You could call it with undefined



          const PromiseOK = <T>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };

          PromiseOK<void>(undefined)


          A better option might be to use overloads to get special behavior for void:



          function PromiseOK(): Promise<void>
          function PromiseOK<T>(val: T): Promise<T>
          function PromiseOK<T>(val?: T): Promise<T> {
          return Promise.resolve(val);
          };

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>


          It is possible to have overloads with arrow functions, but the syntax is not exactly pretty :



          const PromiseOK: {
          (): Promise<void>
          <T>(val: T): Promise<T>
          } = <T>(val?: T): Promise<T> => Promise.resolve(val);

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>





          share|improve this answer


























          • Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

            – Plastic
            Nov 23 '18 at 11:48













          • @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

            – Titian Cernicova-Dragomir
            Nov 23 '18 at 11:53













          • @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

            – Joel
            Feb 8 at 10:13













          • @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

            – Titian Cernicova-Dragomir
            Feb 8 at 10:15











          • @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

            – Joel
            Feb 8 at 10:16



















          0














          It should look like this



          function PromiseOK<T>(val: T): Promise<T> {
          return Promise.resolve(val);
          };


          If you want to keep the fat arrow notation, then



          const PromiseOK = <T extends any>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };


          The notation T extends any also supports void.






          share|improve this answer
























          • thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

            – Plastic
            Nov 23 '18 at 11:17











          • @Plastic Yes, it can be solved like that. But then the function would make no sense?

            – Murat Karagöz
            Nov 23 '18 at 11:51












          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%2f53445422%2ftypescript-generic-promise-return-type%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You implementation seems fine, the problem with void is that the parameter is still expected. You could call it with undefined



          const PromiseOK = <T>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };

          PromiseOK<void>(undefined)


          A better option might be to use overloads to get special behavior for void:



          function PromiseOK(): Promise<void>
          function PromiseOK<T>(val: T): Promise<T>
          function PromiseOK<T>(val?: T): Promise<T> {
          return Promise.resolve(val);
          };

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>


          It is possible to have overloads with arrow functions, but the syntax is not exactly pretty :



          const PromiseOK: {
          (): Promise<void>
          <T>(val: T): Promise<T>
          } = <T>(val?: T): Promise<T> => Promise.resolve(val);

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>





          share|improve this answer


























          • Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

            – Plastic
            Nov 23 '18 at 11:48













          • @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

            – Titian Cernicova-Dragomir
            Nov 23 '18 at 11:53













          • @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

            – Joel
            Feb 8 at 10:13













          • @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

            – Titian Cernicova-Dragomir
            Feb 8 at 10:15











          • @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

            – Joel
            Feb 8 at 10:16
















          2














          You implementation seems fine, the problem with void is that the parameter is still expected. You could call it with undefined



          const PromiseOK = <T>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };

          PromiseOK<void>(undefined)


          A better option might be to use overloads to get special behavior for void:



          function PromiseOK(): Promise<void>
          function PromiseOK<T>(val: T): Promise<T>
          function PromiseOK<T>(val?: T): Promise<T> {
          return Promise.resolve(val);
          };

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>


          It is possible to have overloads with arrow functions, but the syntax is not exactly pretty :



          const PromiseOK: {
          (): Promise<void>
          <T>(val: T): Promise<T>
          } = <T>(val?: T): Promise<T> => Promise.resolve(val);

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>





          share|improve this answer


























          • Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

            – Plastic
            Nov 23 '18 at 11:48













          • @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

            – Titian Cernicova-Dragomir
            Nov 23 '18 at 11:53













          • @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

            – Joel
            Feb 8 at 10:13













          • @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

            – Titian Cernicova-Dragomir
            Feb 8 at 10:15











          • @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

            – Joel
            Feb 8 at 10:16














          2












          2








          2







          You implementation seems fine, the problem with void is that the parameter is still expected. You could call it with undefined



          const PromiseOK = <T>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };

          PromiseOK<void>(undefined)


          A better option might be to use overloads to get special behavior for void:



          function PromiseOK(): Promise<void>
          function PromiseOK<T>(val: T): Promise<T>
          function PromiseOK<T>(val?: T): Promise<T> {
          return Promise.resolve(val);
          };

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>


          It is possible to have overloads with arrow functions, but the syntax is not exactly pretty :



          const PromiseOK: {
          (): Promise<void>
          <T>(val: T): Promise<T>
          } = <T>(val?: T): Promise<T> => Promise.resolve(val);

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>





          share|improve this answer















          You implementation seems fine, the problem with void is that the parameter is still expected. You could call it with undefined



          const PromiseOK = <T>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };

          PromiseOK<void>(undefined)


          A better option might be to use overloads to get special behavior for void:



          function PromiseOK(): Promise<void>
          function PromiseOK<T>(val: T): Promise<T>
          function PromiseOK<T>(val?: T): Promise<T> {
          return Promise.resolve(val);
          };

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>


          It is possible to have overloads with arrow functions, but the syntax is not exactly pretty :



          const PromiseOK: {
          (): Promise<void>
          <T>(val: T): Promise<T>
          } = <T>(val?: T): Promise<T> => Promise.resolve(val);

          PromiseOK() // Promise<void>
          PromiseOK(1) //Promise<number>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 11:52

























          answered Nov 23 '18 at 11:42









          Titian Cernicova-DragomirTitian Cernicova-Dragomir

          72.1k35068




          72.1k35068













          • Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

            – Plastic
            Nov 23 '18 at 11:48













          • @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

            – Titian Cernicova-Dragomir
            Nov 23 '18 at 11:53













          • @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

            – Joel
            Feb 8 at 10:13













          • @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

            – Titian Cernicova-Dragomir
            Feb 8 at 10:15











          • @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

            – Joel
            Feb 8 at 10:16



















          • Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

            – Plastic
            Nov 23 '18 at 11:48













          • @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

            – Titian Cernicova-Dragomir
            Nov 23 '18 at 11:53













          • @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

            – Joel
            Feb 8 at 10:13













          • @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

            – Titian Cernicova-Dragomir
            Feb 8 at 10:15











          • @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

            – Joel
            Feb 8 at 10:16

















          Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

          – Plastic
          Nov 23 '18 at 11:48







          Thanks a lot, I like the idea of using overloads, is it possible to use overloads with the arrow notation?

          – Plastic
          Nov 23 '18 at 11:48















          @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

          – Titian Cernicova-Dragomir
          Nov 23 '18 at 11:53







          @Plastic yes yo can (edited the answer), but the syntax is not exactly great ... I would stick with regular functions for overloads if you don't need the capture this behavior

          – Titian Cernicova-Dragomir
          Nov 23 '18 at 11:53















          @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

          – Joel
          Feb 8 at 10:13







          @TitianCernicova-Dragomir Hi, I am wondering if it possible to use the generic type in the function? Like public promiseOK<T>(endpoint: string, returnType: T): Promise<T> { const promise: Promise<returnType>... returnType is unknown here. cannot find name returnType. I am trying to make a generic wrapper-function. How can I make Promise take it as an argument?

          – Joel
          Feb 8 at 10:13















          @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

          – Titian Cernicova-Dragomir
          Feb 8 at 10:15





          @Joel the type parameter represents the type of returnType you should use that : Promise<T> will work fine in the body of the function.

          – Titian Cernicova-Dragomir
          Feb 8 at 10:15













          @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

          – Joel
          Feb 8 at 10:16





          @TitianCernicova-Dragomir I have asked a question recently where I am explaining a bit more what I am after. Do you mind taking a look? Thanks.

          – Joel
          Feb 8 at 10:16













          0














          It should look like this



          function PromiseOK<T>(val: T): Promise<T> {
          return Promise.resolve(val);
          };


          If you want to keep the fat arrow notation, then



          const PromiseOK = <T extends any>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };


          The notation T extends any also supports void.






          share|improve this answer
























          • thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

            – Plastic
            Nov 23 '18 at 11:17











          • @Plastic Yes, it can be solved like that. But then the function would make no sense?

            – Murat Karagöz
            Nov 23 '18 at 11:51
















          0














          It should look like this



          function PromiseOK<T>(val: T): Promise<T> {
          return Promise.resolve(val);
          };


          If you want to keep the fat arrow notation, then



          const PromiseOK = <T extends any>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };


          The notation T extends any also supports void.






          share|improve this answer
























          • thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

            – Plastic
            Nov 23 '18 at 11:17











          • @Plastic Yes, it can be solved like that. But then the function would make no sense?

            – Murat Karagöz
            Nov 23 '18 at 11:51














          0












          0








          0







          It should look like this



          function PromiseOK<T>(val: T): Promise<T> {
          return Promise.resolve(val);
          };


          If you want to keep the fat arrow notation, then



          const PromiseOK = <T extends any>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };


          The notation T extends any also supports void.






          share|improve this answer













          It should look like this



          function PromiseOK<T>(val: T): Promise<T> {
          return Promise.resolve(val);
          };


          If you want to keep the fat arrow notation, then



          const PromiseOK = <T extends any>(val: T): Promise<T> => {
          return Promise.resolve(val);
          };


          The notation T extends any also supports void.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 11:07









          Murat KaragözMurat Karagöz

          15.4k53771




          15.4k53771













          • thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

            – Plastic
            Nov 23 '18 at 11:17











          • @Plastic Yes, it can be solved like that. But then the function would make no sense?

            – Murat Karagöz
            Nov 23 '18 at 11:51



















          • thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

            – Plastic
            Nov 23 '18 at 11:17











          • @Plastic Yes, it can be solved like that. But then the function would make no sense?

            – Murat Karagöz
            Nov 23 '18 at 11:51

















          thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

          – Plastic
          Nov 23 '18 at 11:17





          thanks for the answer, the thing is if i try to call as PromiseOK() the compiler says that the function expect one argument... it could be solved giving val as a optional parameter??

          – Plastic
          Nov 23 '18 at 11:17













          @Plastic Yes, it can be solved like that. But then the function would make no sense?

          – Murat Karagöz
          Nov 23 '18 at 11:51





          @Plastic Yes, it can be solved like that. But then the function would make no sense?

          – Murat Karagöz
          Nov 23 '18 at 11:51


















          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%2f53445422%2ftypescript-generic-promise-return-type%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()