microsoft / microsoft/TypeScript

generic function parameter infers boolean as true

Open
#29,400 2 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms: true boolean generic function extends default

Code

const fn = <T extends boolean>(flag: T | false = false) => flag
fn({} as boolean)

// The only workaround?
fn<boolean>({} as boolean)

Expected behavior:
Expected fn<boolean>(flag?: boolean) to be inferred

Actual behavior:
Instead got fn<true>(flag?: boolean) as the inferred type.

The issue stems from T | false, which is required for = false to work.

Playground Link: Click here

Related Issues: Possibly https://github.com/Microsoft/TypeScript/issues/28154

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 by reproducing the generic inference example from the issue in the linked TypeScript Playground, then compare it with the related issue #28154. Investigate the type-checker behavior for T | false and default parameters. Done means fn({} as boolean) infers boolean rather than true while preserving the default false behavior.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.