-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
awaited operator doesn't work with type guards #37512
Comments
Probably we have to replace isPromiseLike with isAwaited. if (isPromiseLike(value)) {
internal.status = {
state: State.resolved,
result: value,
};
value.then(
value => {
assert(internal.status.state === State.resolved);
internal.status = {
state: State.fulfilled,
result: value,
};
resume(internal);
},
reason => {
assert(internal.status.state === State.resolved);
internal.status = {
state: State.rejected,
result: reason,
};
resume(internal);
});
}
else {
internal.status = {
state: State.fulfilled,
result: value!, // error
};
resume(internal);
}
https://github.com/falsandtru/spica/blob/v0.0.343/src/promise.ts |
The problem is that |
Let me know if that addresses your concerns so that I can close this issue. |
Basically type arguments are not used in type guards. Can you remove type arguments to accept all types? Actually, your function doesn't accept Ideally, since declare function isAwaited(value: any): value is awaited any; // or awaited typeof value
function f<T>(x: T | awaited T | PromiseLike<T>) {
if (isAwaited(x)) {
x; // awaited T
}
else {
x; // PromiseLike<T>
}
} |
Just to make sure I'm understanding this right, would this be a reasonable implementation of function isAwaited(value: any): value is awaited unknown {
return !value || typeof value.then !== "function";
} |
|
@rbuckton
TypeScript Version: master
Search Terms:
Code
Expected behavior:
The narrowed type of a is awaited a.
Actual behavior:
The narrowed type of a is a.
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: