In this example, I am trying to ensure that the function foo
does not accept a Promise as an argument, but any other type should be acceptable.
export {}
function foo<T>(arg: T extends Promise<unknown> ? never : T) {
console.log(arg);
}
async function bar<T>(arg: Promise<T>) {
foo(await arg); // Why is this causing an error?
}
async function baz(arg: Promise<number>) {
foo(await arg); // This works fine!
}
foo(await Promise.resolve(1)); // No issues here
foo(1); // All good
foo(Promise.resolve(1)); // This should give an error, as expected
When looking at the error in the bar
function:
Argument of type 'Awaited<T>' is not assignable
to parameter of type 'T extends Promise<unknown> ? never : T'.
What exactly sets apart T
from Awaited<T>
?
Is there a solution to make this work correctly, especially when utilizing await with generics?