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
The return type of an async function must be a supertype of Promise #3328
Comments
Created internal issue http://b/130352950 |
You can work around this by annotating class B {
/** @return {*} */
_transform() {
console.log('sorry not sorry')
}
} Closure Compiler infers the return type of This is a general constraint that comes from inferring the return type of a method. You'd also get a type error if |
@lauraharker thank you for the reply, this is a strange one because first of all I forgot that this is OOP so that the method must repeat the signature of its parent, so when I change it to class A extends B {
/** @return {!Promise} */
async _transform() {
console.log('good morning my neighbours')
}
} i get
which is what really supposed to be there in the first place maybe? |
@zavr-1 If you want to be able to extend a sync method to be an async method in a child class, then the initial definition of the method must return a Promise. Otherwise, you're having the overriding method return something different from the original one. Think of it this way. The code that is going to call your class B {
/**
* Behaves synchronously, but returns a `Promise` so it can be overridden with an async version.
* @return {!Promise<undefined>}
*/
_transform() {
console.log('sorry not sorry');
return Promise.resolve();
}
}
class A extends B {
/** @override */
async _transform() {
console.log('good morning my neighbours')
}
}
new A()._transform() |
If a class extends another class where a certain function is sync, with an async function, there will be a warning.
The text was updated successfully, but these errors were encountered: