microsoft / microsoft/TypeScript
Incomprehensible incompatibility around Partial, Conditional types and Generics.
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the generic conditional-type case with the working Partial and concrete-type cases. Trace the compiler's assignability handling for SafePartial and confirm the behavior with a reduced reproduction. Done means the original code compiles without weakening the reported types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100