Skip to content

Proposal: Add an "exclusive or" (^) operator #14094

@mohsen1

Description

@mohsen1

Based on this comment TypeScript does not allow exclusive union types.

I'm proposing a logical or operator similar to union (|) or intersection (&) operators that allows defining types that are one or another.

Code

type Person = { name: string; } ^ { firstname: string; lastname: string; };

const p1: Person = { name: "Foo" };
const p2: Person = { firstname: "Foo", lastname: "Bar" } ;

const bad1: Person = { name: "Foo", lastname: "Bar" }
                                    ~~~~~~~~~~~~~~~   Type Person can not have name and firstname together 
const bad2: Person = { lastname: "Bar", name: "Foo" }                                            
                                        ~~~~~~~~~~~   Type Person can not have lastname and name together

For literal and primitive types it should behave like union type:

// These are the same 
type stringOrNumber = string | number;
type stringORNumber = string ^ number;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions