microsoft / microsoft/TypeScript

`Exclude<Exclude<T, null>, undefined>` is not assignable to `Exclude<T, null | undefined>` and vice versa

Open
#38,142 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: Nightly

Search Terms:
typescript nullness inference

Expected behavior:
They should be assignable to each other.

Actual behavior:
They are not considered assignable to each other.

Type 'Exclude<Exclude<T, null>, undefined>' is not assignable to type 'Exclude<T, null | undefined>'.
  Type 'Exclude<T, null>' is not assignable to type 'Exclude<T, null | undefined>'.
    Type 'T' is not assignable to type 'Exclude<T, null | undefined>'.(2322)

and

Type 'Exclude<T, null | undefined>' is not assignable to type 'Exclude<Exclude<T, null>, undefined>'.
  Type 'T' is not assignable to type 'Exclude<Exclude<T, null>, undefined>'.(2322)

Related Issues:
N/A

Code

export function f<T>(v: Exclude<Exclude<T, null>, undefined>) {
    const w: Exclude<T, null | undefined> = v;
}

export function g<T>(v: Exclude<T, null | undefined>) {
    const w: Exclude<Exclude<T, null>, undefined> = v;
}

Output
export function f(v) {
    const w = v;
}
export function g(v) {
    const w = v;
}

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

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 provided Playground link and the generic functions f and g under the listed strict compiler options. Trace how the compiler checks the two nested Exclude types, then verify that both assignments are accepted without the reported TS2322 errors while preserving the shown emitted output.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.