Skip to content

Report errors if union subtype reduction results in fewer types than were explicitly written #3862

Closed
@danquirk

Description

@danquirk
// A function that accepts strings or non-primitive types
function foo(x: Object | string) { ... }
foo('hello'); // ok
foo({ a: 3 }); // ok
foo(1); // ok, meant for this to be an error

Based on examples seen in DefinitelyTyped. Someone misunderstood what Object means, thinking it meant non-primitive types (not an uncommon source of confusion), so they union it with the one primitive type they do want to allow. The compiler performs subtype reduction and ends up with only Object. This is now allowing code the user intended to disallow. At this point if the compiler knows it has reduced an explicitly written union type to a set of types smaller than what was originally written it should just issue an error to alert the user that some part of their type annotation is not having the effect they expect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions