Skip to content

Consider future syntax and semantics of enum inheritance #22

@lazytype

Description

@lazytype

In TypeScript, sometimes one wants to define an enum based on another enum(s), whose common members may or may not be found to be nominally distinct. Consider these simple examples:
Existing workaround 1:

enum Foo {
  A = 'a',
}

enum Bar {
  B = 'b',
}

const FooBar = {
  ...Foo,
  ...Bar,
  // Additional members can be defined here
};
type FooBar = Foo | Bar;

// FooBar.A is assignable to Foo.A - not nominally distinct

Existing non-solution:

const FooBar = {
  A = Foo.A,
  B = Foo.B,
};

// May ensure that all members of FooBar are members of Foo and Bar, but not the converse.

Potential first-class syntax 1:

enum FooBar {
  ...Foo,
  ...Bar,
 // Additional members can be defined here
}

Potential first-class syntax 2:

enum FooBar extends Foo, Bar {
  // Additional members can be defined here
}  // introduce multiple inheritance syntax to the language

Potential first-class syntax 3:

enum FooBar extends Foo {  // Only allow single inheritance
  B = Bar.B,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions