-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async/await can not be used in class method definition(without method body) #22035
Comments
can not change title... here "class method" is not meaning static method... my sample is not also... should be instance/prototype function(method), I guess, for static method of class should have the same issue. |
Your usage is totally wrong. The The The EDIT: If you want an In addition, for an One more thing, you should post your problem to StackOverflow if you don't really understand some feature or don't know why it doesn't work. The GitHub Issue Tracker should only be used when you are at least 90% sure that there is a bug in the project. |
thanks @yume-chan for your reply. I knew async keywords will wrap function return value as Promise if it is not. here just a sample to demonstrate. the key thing here is
if this is ms offically reply, I will close this issue (I reserve my opinion). |
thanks @andy-ms 's response. |
Again, your opinion is totally WRONG.
The async function doSomething() {
await Promise.resolve("OK");
alert("done");
} Is semantically equals to function doSomething() {
Promise.resolve("OK").then(() => {
alert("done");
});
} So as I have said above, your field class BaseClass {
created: () => Promise<string>; // NO async here
}
BaseClass.prototype.created = async () => { // async HERE
retrun await Promise.resolve("OK");
} Or, you should just declare your function body in the class class BaseClass {
async created(): Promise<string> {
retrun await Promise.resolve("OK");
}
} Which I have also said above. (Why don't you do this when you are already using |
Hi, yume-chan , I guess you still don't understand my question. [created] here is not a field. it is method with itself method signature... I think I need to reopen this question. need senior stuff to check it again. } |
Please check ecma-262 chapter 14.6 Class Definitions, if it's a method definition, it must follow the syntax defined in 14.3 Method Definitions, which requires a parameter list and a method body. I cannot see both on your Class fields is a TypeScript extension and a stage 3 proposal tc39/proposal-class-fields, your If you cannot read ecma-262, I think there is nothing more we can talk. I have unsubscribed this issue. |
@vincedan if you want to declare a method, do it inline like @yume-chan showed: class BaseClass {
async created() {
return await Promise.resolve("OK");
}
} If you want an instance property that is an async function, do it like this: class BaseClass {
created = async () => await Promise.resolve("OK");
} If you are trying to create an abstract base class to implement in other classes, do it like this: abstract class BaseClass {
abstract created(): Promise<string>;
}
class DerivedClass extends BaseClass {
async created() {
return await Promise.resolve("OK");
}
} |
@yortus thanks. I need abstract interface defined here. I wanna use typescript strong type Intellisense but without method body. the third solution give me another hint. thanks again. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
`class BaseClass {
async created: () => Promise< string >;
}
BaseClass.prototype.created = () => {
return await Promise.resolve("OK");
};`
Severity Code Description Project File
Error TS1042 'async' modifier cannot be used here. TypeScript Virtual Projects
Error TS2322 Type '() => string' is not assignable to type '() => Promise'. Type 'string' is not assignable to type 'Promise'.
Error TS1308 'await' expression is only allowed within an async function. TypeScript Virtual Projects
The text was updated successfully, but these errors were encountered: