GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `===` narrowing of a primitive union now produces a non-widening literal type (regression from microsoft/typescript-go#4781) · Issue #64766 · microsoft/TypeScript

=== narrowing of a primitive union now produces a non-widening literal type (regression from microsoft/typescript-go#4781) #64766

New issue
Copy link
New issue
Copy link
Open
#64771
Open
=== narrowing of a primitive union now produces a non-widening literal type (regression from microsoft/typescript-go#4781)#64766
#64771
Copy link
Labels
BugA bug in TypeScript
Milestone
TypeScript 7.1.1 RC

Description

@sophiebits
Sophie Alpert (sophiebits)
opened on Oct 13, 2026
Issue body actions

🔎 Search Terms

narrowing equality literal widening, non-widening literal, narrowTypeByEquality, isUniformUnionType, TS2322 narrowed let, flatMap union inference 7.1

🕗 Version & Regression Information

  • This changed in commit or PR Optimize narrowTypeByEquality and narrowTypeBySwitchOnDiscriminant typescript-go#4781 (322fa1ec). I built tsgo at its parent commit (98c47847) and at 322fa1ec. Only 322fa1ec reports the errors below.
  • This changed between versions 7.1.0-dev.20260730.1 and 7.1.0-dev.20260731.1. It still reproduces on 7.1.0-dev.20261012.1. TypeScript 6.0.3 and 7.0.2 report no errors (same code on 7.0.2).

⏯ Playground Link

TypeScript Go Playground, nightly 2026.10.13

💻 Code

(--strict)

declare const kind: string | undefined;

if (kind === "a") {
    let copy = kind;
    copy = "b";

    const obj = { kind };
    obj.kind = "b";

    const arr = [kind];
    arr.push("b");
}

declare const count: number | undefined;

if (count === 1) {
    let copy = count;
    copy = 2;
}

🙁 Actual behavior

main.ts(5,5): error TS2322: Type '"b"' is not assignable to type '"a"'.
main.ts(8,5): error TS2322: Type '"b"' is not assignable to type '"a"'.
main.ts(11,14): error TS2345: Argument of type '"b"' is not assignable to parameter of type '"a"'.
main.ts(18,5): error TS2322: Type '2' is not assignable to type '1'.

The narrowed kind now has the non-widening literal type "a". So let, object literal properties and array literals initialised from it are typed "a" instead of string.

🙂 Expected behavior

No errors, matching 6.0.3, 7.0.2 and the commit before microsoft/typescript-go#4781. Inside the if, kind should have the widening literal type "a", so mutable locations widen to string.

Additional information about the issue

(This bug report was assembled by AI, but I reviewed it prior to posting.)

  • I think the cause is the fast path Optimize narrowTypeByEquality and narrowTypeBySwitchOnDiscriminant typescript-go#4781 added to narrowTypeByEquality. When the declared type passes the new isUniformUnionType check (a primitive union such as string | undefined), it returns getRegularTypeOfLiteralType(valueType), the non-widening form of the literal. unionContainsType counts "a" as contained in string | undefined because the union includes string, so this path runs here. The old path, replacePrimitivesWithLiterals(filteredType, valueType), returned the comparison's widening literal. A plain string declared type isn't a union, so it isn't affected. Perhaps the fast path should only apply when the union contains the literal itself, but I haven't tested that.
  • switch (kind) { case "a": ... } already produced a non-widening literal in 6.0.3 and 7.0.2. If === matching switch is intended, it's still a breaking change. Optimize narrowTypeByEquality and narrowTypeBySwitchOnDiscriminant typescript-go#4781 is described as a performance optimization, and its top-400-repos run came back clean.
  • Real-world impact: in our codebase this broke flatMap inference, because the branches of the callback no longer unify:
    declare const requests: Array<{ parameterPath?: string }>;
    requests.flatMap((request) => {
        if (request.parameterPath === "lookupFields") return [{ parameterPath: request.parameterPath }];
        if (request.parameterPath === "recordFields") return [{ parameterPath: request.parameterPath }];
        return [];
    }); // TS2345: '"recordFields"' is not assignable to '"lookupFields"'
Reacted by snarbles2

Activity

  1. Ryan Cavanaugh (RyanCavanaugh)
    added
    BugA bug in TypeScript
    and removed
    BugA bug in TypeScript
    on Oct 13, 2026
  2. Ryan Cavanaugh (RyanCavanaugh)
    added this to the TypeScript 7.1.1 RC milestone on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      • TypeScript 7.1.1 RCNo due date

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.