Skip to content

Request for StrictUnion (aka either this or that) #76

Closed
@balupton

Description

@balupton

Would be nice to see this typescript library add the StrictUnion generic type

type UnionKeys<T> = T extends any ? keyof T : never;
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
type StrictUnion<T> = StrictUnionHelper<T, T>

type A = { a: number }
type B = { b: number }

const unionList: Array<A | B> = [{ a: 1 }, { b: 2 }]
const unionListInvalid: Array<A | B> = [{ a: 1 }, { b: 2 }, { a: 1, b: 2 }]
// ^ not actually invalid

const strictUnionList: Array<StrictUnion<A | B>> = [{ a: 1 }, { b: 2 }]
const strictUnionListInvalid: Array<StrictUnion<A | B>> = [{ a: 1 }, { b: 2 }, { a: 1, b: 2 }]
// ^ invalid as expected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions