Why are my getter/setter not compiling in TypeScript?
up vote
2
down vote
favorite
I have a class with the following getter/setter defined:
class ConfigStoreController {
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
set debugMode(value: string) {
(async () => await this.SetConfigParameter("debugMode", value))();
}
private async GetConfigParameter(parameter: string) {
return await RX.Storage.getItem(parameter);
}
private async SetConfigParameter(param: string, value: string) {
return await RX.Storage.setItem(param, value);
}
}
export default new ConfigStoreController();
The getter is marked as incorrect because of the setter, that is, if the latter is removed, then the getter is OK.
The error is the following:
return (async () => await this.GetConfigParameter("debugMode"))();
error TS2322: Type 'Promise' is not assignable to type 'string'.
To be clear, the getter alone compiles fine:
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
What is wrong with my code?
typescript async-await getter-setter
add a comment |
up vote
2
down vote
favorite
I have a class with the following getter/setter defined:
class ConfigStoreController {
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
set debugMode(value: string) {
(async () => await this.SetConfigParameter("debugMode", value))();
}
private async GetConfigParameter(parameter: string) {
return await RX.Storage.getItem(parameter);
}
private async SetConfigParameter(param: string, value: string) {
return await RX.Storage.setItem(param, value);
}
}
export default new ConfigStoreController();
The getter is marked as incorrect because of the setter, that is, if the latter is removed, then the getter is OK.
The error is the following:
return (async () => await this.GetConfigParameter("debugMode"))();
error TS2322: Type 'Promise' is not assignable to type 'string'.
To be clear, the getter alone compiles fine:
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
What is wrong with my code?
typescript async-await getter-setter
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a class with the following getter/setter defined:
class ConfigStoreController {
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
set debugMode(value: string) {
(async () => await this.SetConfigParameter("debugMode", value))();
}
private async GetConfigParameter(parameter: string) {
return await RX.Storage.getItem(parameter);
}
private async SetConfigParameter(param: string, value: string) {
return await RX.Storage.setItem(param, value);
}
}
export default new ConfigStoreController();
The getter is marked as incorrect because of the setter, that is, if the latter is removed, then the getter is OK.
The error is the following:
return (async () => await this.GetConfigParameter("debugMode"))();
error TS2322: Type 'Promise' is not assignable to type 'string'.
To be clear, the getter alone compiles fine:
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
What is wrong with my code?
typescript async-await getter-setter
I have a class with the following getter/setter defined:
class ConfigStoreController {
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
set debugMode(value: string) {
(async () => await this.SetConfigParameter("debugMode", value))();
}
private async GetConfigParameter(parameter: string) {
return await RX.Storage.getItem(parameter);
}
private async SetConfigParameter(param: string, value: string) {
return await RX.Storage.setItem(param, value);
}
}
export default new ConfigStoreController();
The getter is marked as incorrect because of the setter, that is, if the latter is removed, then the getter is OK.
The error is the following:
return (async () => await this.GetConfigParameter("debugMode"))();
error TS2322: Type 'Promise' is not assignable to type 'string'.
To be clear, the getter alone compiles fine:
get debugMode() {
return (async () => await this.GetConfigParameter("debugMode"))();
}
What is wrong with my code?
typescript async-await getter-setter
typescript async-await getter-setter
edited Nov 20 at 12:51
asked Nov 20 at 12:39
Olivier MATROT
1,26821841
1,26821841
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Since your getter contains an async operation there is no way to have it directly retrun string
. It does not matter if you wrap the async operation in an anonymous function, that function call will return a Promise<string>
. This means the type of the property will be Promise<string>
The getter alone works, because the property type would be Promise<string>
which is not a problem. It stops working if you also have a setter and the parameter of the setter does not have the same type as the return of the get
.
You can create property that is a Promise<string>
instead.
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
set debugMode(value: Promise<string>) {
// There is no way to wait for this operation to finish from the outside, this might be an issue
// Also unhandled errors from the anonymous method are not handled and are not propagated to the caller, since the set is async
(async () => this.SetConfigParameter("debugMode", await value))();
}
}
A better solution is probably to leave the getter and have a set
method instead:
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
async setDebugMode(value: string) {
this.SetConfigParameter("debugMode", await value)
}
}
add a comment |
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',
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%2f53393200%2fwhy-are-my-getter-setter-not-compiling-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Since your getter contains an async operation there is no way to have it directly retrun string
. It does not matter if you wrap the async operation in an anonymous function, that function call will return a Promise<string>
. This means the type of the property will be Promise<string>
The getter alone works, because the property type would be Promise<string>
which is not a problem. It stops working if you also have a setter and the parameter of the setter does not have the same type as the return of the get
.
You can create property that is a Promise<string>
instead.
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
set debugMode(value: Promise<string>) {
// There is no way to wait for this operation to finish from the outside, this might be an issue
// Also unhandled errors from the anonymous method are not handled and are not propagated to the caller, since the set is async
(async () => this.SetConfigParameter("debugMode", await value))();
}
}
A better solution is probably to leave the getter and have a set
method instead:
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
async setDebugMode(value: string) {
this.SetConfigParameter("debugMode", await value)
}
}
add a comment |
up vote
1
down vote
accepted
Since your getter contains an async operation there is no way to have it directly retrun string
. It does not matter if you wrap the async operation in an anonymous function, that function call will return a Promise<string>
. This means the type of the property will be Promise<string>
The getter alone works, because the property type would be Promise<string>
which is not a problem. It stops working if you also have a setter and the parameter of the setter does not have the same type as the return of the get
.
You can create property that is a Promise<string>
instead.
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
set debugMode(value: Promise<string>) {
// There is no way to wait for this operation to finish from the outside, this might be an issue
// Also unhandled errors from the anonymous method are not handled and are not propagated to the caller, since the set is async
(async () => this.SetConfigParameter("debugMode", await value))();
}
}
A better solution is probably to leave the getter and have a set
method instead:
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
async setDebugMode(value: string) {
this.SetConfigParameter("debugMode", await value)
}
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Since your getter contains an async operation there is no way to have it directly retrun string
. It does not matter if you wrap the async operation in an anonymous function, that function call will return a Promise<string>
. This means the type of the property will be Promise<string>
The getter alone works, because the property type would be Promise<string>
which is not a problem. It stops working if you also have a setter and the parameter of the setter does not have the same type as the return of the get
.
You can create property that is a Promise<string>
instead.
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
set debugMode(value: Promise<string>) {
// There is no way to wait for this operation to finish from the outside, this might be an issue
// Also unhandled errors from the anonymous method are not handled and are not propagated to the caller, since the set is async
(async () => this.SetConfigParameter("debugMode", await value))();
}
}
A better solution is probably to leave the getter and have a set
method instead:
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
async setDebugMode(value: string) {
this.SetConfigParameter("debugMode", await value)
}
}
Since your getter contains an async operation there is no way to have it directly retrun string
. It does not matter if you wrap the async operation in an anonymous function, that function call will return a Promise<string>
. This means the type of the property will be Promise<string>
The getter alone works, because the property type would be Promise<string>
which is not a problem. It stops working if you also have a setter and the parameter of the setter does not have the same type as the return of the get
.
You can create property that is a Promise<string>
instead.
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
set debugMode(value: Promise<string>) {
// There is no way to wait for this operation to finish from the outside, this might be an issue
// Also unhandled errors from the anonymous method are not handled and are not propagated to the caller, since the set is async
(async () => this.SetConfigParameter("debugMode", await value))();
}
}
A better solution is probably to leave the getter and have a set
method instead:
class ConfigStoreController {
GetConfigParameter(p: string): Promise<string> {
return Promise.resolve(p)
}
SetConfigParameter(p: string, value: string): Promise<void> {
return Promise.resolve(void 0)
}
get debugMode() {
return this.GetConfigParameter("debugMode");
}
async setDebugMode(value: string) {
this.SetConfigParameter("debugMode", await value)
}
}
answered Nov 20 at 12:49
Titian Cernicova-Dragomir
55.2k33351
55.2k33351
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53393200%2fwhy-are-my-getter-setter-not-compiling-in-typescript%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