Skip to content

Broken narrowing of types intersecting with unique symbols #24846

Closed

Description

TypeScript Version: 3.0.0-dev.20180609 & 2.9.1

Search Terms:
Narrowing unique symbol union

Code

declare const phantom: unique symbol;
type Phantom = typeof phantom;
type PhantomString = string & Phantom;

const fn = (arg?: PhantomString) => arg;
const fn2 = (arg: Array<PhantomString | null>) => arg;

Expected behavior:

fn and fn2 should be reported as:

const fn: (arg?: PhantomString) => PhantomString | undefined;
const fn2: (arg: Array<PhantomString | null>) => Array<PhantomString | null>;

Actual behavior:

fn and fn2 are reported as:

const fn: (arg?: undefined) => undefined;
const fn2: (arg: null[]) => null[];

Playground Link:

https://www.typescriptlang.org/play/#src=declare%20const%20phantom%3A%20unique%20symbol%3B%0D%0Atype%20Phantom%20%3D%20typeof%20phantom%3B%0D%0Atype%20PhantomString%20%3D%20string%20%26%20Phantom%3B%0D%0A%0D%0Aconst%20fn%20%3D%20(arg%3F%3A%20PhantomString)%20%3D%3E%20arg%3B%0D%0A%0D%0Aconst%20fn2%20%3D%20(arg%3A%20Array%3CPhantomString%20%7C%20null%3E)%20%3D%3E%20arg%3B

Appears to be partially related to strictNullChecks

Activity

  1. jack-williams commented on Jun 11, 2018

    @jack-williams
    Collaborator

    Related #23672 ?

  2. mhegazy commented on Jun 11, 2018

    @mhegazy
    Contributor

    The compiler reduces empty intersection types to never when used in a union. an empty intersection type is one whose constituents are all unit types. so 1 & 2 "a" & false or "s" & unique symbol.

    so PhantomString | null is just null. see #18438 for more details.

  3. leemhenson commented on Jun 12, 2018

    @leemhenson
    Author

    Ah ok, I see. Using type PhantomString = string & { "phantom-string": any }; seems to work. I can see there are a few issues suggesting true nominal typing support so I'll add my 👍 to those.

    Thanks.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions