In the following sample, the I do not want the function foo accepting a Promise, but any other type should be OK.
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 an error?
}
async function baz(arg: Promise<number>) {
foo(await arg); // OK!
}
foo(await Promise.resolve(1)); // OK
foo(1); // OK
foo(Promise.resolve(1)); // Not OK, as intended
In the bar function, the error is:
Argument of type 'Awaited<T>' is not assignable
to parameter of type 'T extends Promise<unknown> ? never : T'.
What is the difference between T and Awaited<T>?
Is there any way to make this work as expected, when using await on a generic?