TypeScript
TypeScript copied to clipboard
Incomprehensible incompatibility around Partial, Conditional types and Generics.
TypeScript Version: 3.9.0-dev.20200322
Search Terms: partial, conditional types
Code
type SafePartial<T> = T extends {} ? Partial<T> : any;
interface QB<TRecord extends {}> {
insert(record: SafePartial<TRecord>): void;
}
async function insert1<TRecord extends {}>(qb: QB<TRecord>, record: TRecord) {
await qb.insert(record);
}
Expected behavior:
The code successfully compiles
Actual behavior:
Following error is observed for qb.insert(record):
Argument of type 'TRecord' is not assignable to parameter of type 'SafePartial<TRecord>'. Type '{}' is not assignable to type 'SafePartial<TRecord>'.
The issue goes away if I use Partial instead of the SafePartial above.
The issue also goes away if a concrete type is used instead of generic type:
interface User {
id: string;
}
async function insert1(qb: QB<User>, record: User) {
await qb.insert(record);
}
Playground Link: Playground
Similar/same issue as open bug for ts-essentials library:
export type Test<T> = T extends number // doesn't really matter what the condition is
? Partial<T>
: Partial<T>;
function conditionalPartialTestErrors<T>(): Test<T> {
return {}; // error
}
function conditionalPartialTestCompiles<T>(): Partial<T> | Test<T> {
return {}; // no error
}
I think it works if we remove Distribution from the equation and hence it will "resolve" sooner rather than at the instantiation step.
type Test<T> = [T] extends number? Partial<T> : Partial<T>
function conditionalPartialTestErrors<T>(){
return (state: Test<T> = {}) => {
}
}
function conditionalPartialTestCompiles<T>(){
return (state: Test<T> = {}) => {
}
}
Not sure though why one would "resolve" sooner and the other one would have to be deferred till instantiation.
I found a repro without distribution:
type C<A> = [A] extends [{}] ? { a: string } : { a: number };
<A extends {}>(): C<A> => ({ a: 'x' });
// Error: Type '{ a: string; }' is not assignable to type 'C<A>'.(2322)
seems Partial is unrelated
The compiler cannot determine assignability for conditional types when either T or U is not fully defined, preventing it from resolving T extends U ? V : W.
Here's a great example explaining this in detail by jcalz https://stackoverflow.com/a/56014202/10310568