microsoft / microsoft/TypeScript

Exclude<keyof T, 'x'> should be assignable to Exclude<Exclude<keyof T, 'x'>, 'x'>

Open
#37,768 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Conditional Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.3

Search Terms:

  • type Exclude keyof is not assignable to type Exclude Exclude keyof
  • exclude idempotent

Code

const f = <T>(arg: Omit<T, 'field'>) => {
    const a = { ...arg, field: 'value' };
    const { field, ...b } = a;
    const c: Omit<T, 'field'> = a;
    const d: Omit<T, 'field'> = b;
    return { c, d };
};

Expected behavior:
Code compiles.

Actual behavior:
Compilation error on d initialization (but not on c initialization):

tmp.ts:5:11 - error TS2322: Type 'Pick<Pick<T, Exclude<keyof T, "field">> & { field: string; }, Exclude<Exclude<keyof T, "field">, "field">>' is not assignable to type 'Pick<T, Exclude<keyof T, "field">>'.
  Type 'Exclude<keyof T, "field">' is not assignable to type 'Exclude<Exclude<keyof T, "field">, "field">'.
    Type 'keyof T' is not assignable to type 'Exclude<keyof T, "field">'.

5     const d: Omit<T, 'field'> = b;
            ~


Found 1 error.

Playground Link: Playground Link

Related Issues: I did not find other related issues.

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

Reproduce the generic Omit/Exclude example in the linked TypeScript Playground using the reported TypeScript 3.8.3 behavior, then compare it with the latest nightly. Trace the type-checking path for the failing assignment to determine why the nested Exclude is rejected; done means the sample compiles and has a regression test covering the case.

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.