microsoft / microsoft/TypeScript

Incomprehensible incompatibility around Partial, Conditional types and Generics.

Open
#37,524 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.