microsoft / microsoft/TypeScript
Unable to return ElementFinder from an async function in TS 3.9
@rbuckton ya está trabajando en esto.
Desde el 19/10/2020.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
TypeScript Version: 3.9.2
Search Terms:
The return type of an async function must either be a valid promise or must not contain a callable 'then' member.(1058)
Code
export declare class ElementFinder {
then?: (fn: (value: any) => any, errorFn?: (error: any) => any) => Promise<any>;
}
export declare class ElementArray{
first(): ElementFinder;
}
export async function assertSingleElement( // error: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.(1058)
array: ElementArray,
): Promise<ElementFinder> {
//await something up here so require async function
return array.first() as any;
}
This code works in 3.8.
Expected behavior:
We should be able to compile the above code or at least tell TS to ignore the error and compile anyway.
Actual behavior:
There is no way that I could find to return an object with a then function from an async function.
This is ultimately an issue with Protractor. The "workaround" is to edit the d.ts files in node_modules which for most build chains is not an option at all. Related issue:
https://github.com/angular/protractor/issues/3831
In the above function I am trying to return an ElementFinder object from an async function. ElementFinder is a badly typed Promise type thing but I don't want the ElementFinder to be resolved, I want an unresolved ElementFinder to be returned and to be used in the calling function. It seems that the then function is breaking TS and there doesn't seem to be any way to trick TS into allowing this.
This worked fine in 3.8 but breaks in 3.9 and currently preventing us from upgrading to Angular 10.
How to I trick TS into allowing this?
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Evaluación
Este issue todavía no se ha evaluado.