TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Allow "T extends enum" generic constraint

Open IanKemp opened this issue 6 years ago • 39 comments

TypeScript has a discrete enum type that allows various compile-time checks and constraints to be enforced when using such types. It would be extremely useful to allow generic constraints to be limited to enum types - currently the only way to do this is via T extends string | number which neither conveys the intent of the programmer, nor imposes the requisite type enforcement.

export enum StandardSortOrder {
    Default,
    Most,
    Least
}

export enum AlternativeSortOrder {
    Default,
    High,
    Medium,
    Low
}

export interface IThingThatUsesASortOrder<T extends enum> { // doesn't compile
    sortOrder: T;
}

IanKemp avatar Mar 27 '19 08:03 IanKemp