Typescript Generic Promise Return Type
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
add a comment |
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
add a comment |
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
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
typescript generics promise typescript-generics
asked Nov 23 '18 at 11:00
PlasticPlastic
1,94521632
1,94521632
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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>
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 capturethis
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? Likepublic 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 ofreturnType
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
add a comment |
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
.
thanks for the answer, the thing is if i try to call asPromiseOK()
the compiler says that the function expect one argument... it could be solved givingval
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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 capturethis
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? Likepublic 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 ofreturnType
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
add a comment |
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>
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 capturethis
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? Likepublic 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 ofreturnType
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
add a comment |
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>
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>
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 capturethis
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? Likepublic 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 ofreturnType
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
add a comment |
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 capturethis
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? Likepublic 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 ofreturnType
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
add a comment |
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
.
thanks for the answer, the thing is if i try to call asPromiseOK()
the compiler says that the function expect one argument... it could be solved givingval
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
add a comment |
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
.
thanks for the answer, the thing is if i try to call asPromiseOK()
the compiler says that the function expect one argument... it could be solved givingval
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
add a comment |
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
.
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
.
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 asPromiseOK()
the compiler says that the function expect one argument... it could be solved givingval
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
add a comment |
thanks for the answer, the thing is if i try to call asPromiseOK()
the compiler says that the function expect one argument... it could be solved givingval
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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