Skip to content

Broken narrowing of types intersecting with unique symbols #24846

Closed
@leemhenson

Description

@leemhenson

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

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