Skip to content

Omit behaves poorly with unions #39556

@bakkot

Description

@bakkot

TypeScript Version: 3.9.2 (also v4.0.0-beta)

Search Terms: omit, pick, union

Code

type StringTok = { name: 'string', location: number, contents: string }
type NumberTok = { name: 'number', location: number, value: number }

type Tok = StringTok | NumberTok

type UnlocatedTok = Omit<Tok, 'location'>

let a: UnlocatedTok = { name: 'number', value: 1 }

let b: UnlocatedTok = { name: 'number' }

Expected behavior:
No type error on the let a, type error on the let b.

Actual behavior:

Type error on the let a:

Type '{ name: "number"; value: number; }' is not assignable to type 'Pick<Tok, "name">'.
Object literal may only specify known properties, and 'value' does not exist in type 'Pick<Tok, "name">'.(2322)

No type error on the let b.

Specifically, it is surprising to me that, contrary to its description, the result of Omit<T, K> is not the type which contains the members of T except without the properties in K, but instead some other type.

Playground Link: Link

Related Issues: None that I could find.

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