ts-enum-util
Advanced tools
Comparing version 2.0.3 to 3.0.0
@@ -83,2 +83,3 @@ "use strict"; | ||
EnumWrapper.prototype.keys = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -103,3 +104,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -115,2 +115,3 @@ /** | ||
EnumWrapper.prototype.values = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -135,3 +136,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -144,2 +144,3 @@ /** | ||
EnumWrapper.prototype.entries = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -165,3 +166,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -168,0 +168,0 @@ /** |
@@ -81,2 +81,3 @@ /** | ||
EnumWrapper.prototype.keys = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -101,3 +102,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -113,2 +113,3 @@ /** | ||
EnumWrapper.prototype.values = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -133,3 +134,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -142,2 +142,3 @@ /** | ||
EnumWrapper.prototype.entries = function () { | ||
var _a; | ||
var _this = this; | ||
@@ -163,3 +164,2 @@ var index = 0; | ||
_a; | ||
var _a; | ||
}; | ||
@@ -166,0 +166,0 @@ /** |
@@ -11,2 +11,8 @@ /** | ||
/** | ||
* Extracts only keys of type T that are assignable to type `string`. | ||
* This is necessary starting with TypeScript 2.9 because keyof T can now | ||
* include `number` and `symbol` types. | ||
*/ | ||
type StringKeyOf<T> = Extract<keyof T, string>; | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
@@ -24,3 +30,3 @@ * Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
*/ | ||
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<EnumWrapper.Entry<V, T>> { | ||
export declare class EnumWrapper<V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> implements Iterable<EnumWrapper.Entry<V, T>>, ArrayLike<EnumWrapper.Entry<V, T>> { | ||
private readonly enumObj; | ||
@@ -78,3 +84,3 @@ /** | ||
*/ | ||
static getCachedInstance<T extends EnumLike<number, keyof T>>(enumObj: T): EnumWrapper<number, T>; | ||
static getCachedInstance<T extends EnumLike<number, StringKeyOf<T>>>(enumObj: T): EnumWrapper<number, T>; | ||
/** | ||
@@ -88,3 +94,3 @@ * Gets a cached EnumWrapper for an enum-like object with string values. | ||
*/ | ||
static getCachedInstance<T extends EnumLike<string, keyof T>>(enumObj: T): EnumWrapper<string, T>; | ||
static getCachedInstance<T extends EnumLike<string, StringKeyOf<T>>>(enumObj: T): EnumWrapper<string, T>; | ||
/** | ||
@@ -98,3 +104,3 @@ * Gets a cached EnumWrapper for an enum-like object with a mixture of number and string values. | ||
*/ | ||
static getCachedInstance<T extends EnumLike<number | string, keyof T>>(enumObj: T): EnumWrapper<number | string, T>; | ||
static getCachedInstance<T extends EnumLike<number | string, StringKeyOf<T>>>(enumObj: T): EnumWrapper<number | string, T>; | ||
/** | ||
@@ -121,3 +127,3 @@ * Create a new EnumWrapper instance. | ||
*/ | ||
keys(): IterableIterator<keyof T>; | ||
keys(): IterableIterator<StringKeyOf<T>>; | ||
/** | ||
@@ -131,3 +137,3 @@ * Get an iterator for this enum's values. | ||
*/ | ||
values(): IterableIterator<T[keyof T]>; | ||
values(): IterableIterator<T[StringKeyOf<T>]>; | ||
/** | ||
@@ -171,3 +177,3 @@ * Get an iterator for this enum's entries as [key, value] tuples. | ||
*/ | ||
getKeys(): (keyof T)[]; | ||
getKeys(): (StringKeyOf<T>)[]; | ||
/** | ||
@@ -180,3 +186,3 @@ * Get a list of this enum's values. | ||
*/ | ||
getValues(): T[keyof T][]; | ||
getValues(): T[StringKeyOf<T>][]; | ||
/** | ||
@@ -194,3 +200,3 @@ * Get a list of this enum's entries as [key, value] tuples. | ||
*/ | ||
isKey(key: string | null | undefined): key is keyof T; | ||
isKey(key: string | null | undefined): key is StringKeyOf<T>; | ||
/** | ||
@@ -203,3 +209,3 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
asKeyOrThrow(key: string | null | undefined): keyof T; | ||
asKeyOrThrow(key: string | null | undefined): StringKeyOf<T>; | ||
/** | ||
@@ -213,3 +219,3 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey: keyof T): keyof T; | ||
asKeyOrDefault(key: string | null | undefined, defaultKey: StringKeyOf<T>): StringKeyOf<T>; | ||
/** | ||
@@ -223,3 +229,3 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
asKeyOrDefault(key: string | null | undefined, defaultKey?: keyof T): keyof T | undefined; | ||
asKeyOrDefault(key: string | null | undefined, defaultKey?: StringKeyOf<T>): StringKeyOf<T> | undefined; | ||
/** | ||
@@ -249,3 +255,3 @@ * Casts a string to a properly-typed key for this enum. | ||
*/ | ||
isValue(value: V | null | undefined): value is T[keyof T]; | ||
isValue(value: V | null | undefined): value is T[StringKeyOf<T>]; | ||
/** | ||
@@ -258,3 +264,3 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
asValueOrThrow(value: V | null | undefined): T[keyof T]; | ||
asValueOrThrow(value: V | null | undefined): T[StringKeyOf<T>]; | ||
/** | ||
@@ -268,3 +274,3 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue: T[keyof T]): T[keyof T]; | ||
asValueOrDefault(value: V | null | undefined, defaultValue: T[StringKeyOf<T>]): T[StringKeyOf<T>]; | ||
/** | ||
@@ -278,3 +284,3 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
asValueOrDefault(value: V | null | undefined, defaultValue?: T[keyof T]): T[keyof T] | undefined; | ||
asValueOrDefault(value: V | null | undefined, defaultValue?: T[StringKeyOf<T>]): T[StringKeyOf<T>] | undefined; | ||
/** | ||
@@ -307,3 +313,3 @@ * Casts a value to a properly-typed value for this enum. | ||
*/ | ||
getKeyOrThrow(value: V | null | undefined): keyof T; | ||
getKeyOrThrow(value: V | null | undefined): StringKeyOf<T>; | ||
/** | ||
@@ -319,3 +325,3 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey: keyof T): keyof T; | ||
getKeyOrDefault(value: V | null | undefined, defaultKey: StringKeyOf<T>): StringKeyOf<T>; | ||
/** | ||
@@ -331,3 +337,3 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
getKeyOrDefault(value: V | null | undefined, defaultKey?: keyof T): keyof T | undefined; | ||
getKeyOrDefault(value: V | null | undefined, defaultKey?: StringKeyOf<T>): StringKeyOf<T> | undefined; | ||
/** | ||
@@ -362,3 +368,3 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
*/ | ||
getValueOrThrow(key: string | null | undefined): T[keyof T]; | ||
getValueOrThrow(key: string | null | undefined): T[StringKeyOf<T>]; | ||
/** | ||
@@ -372,3 +378,3 @@ * Gets the enum value for the provided key. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue: T[keyof T]): T[keyof T]; | ||
getValueOrDefault(key: string | null | undefined, defaultValue: T[StringKeyOf<T>]): T[StringKeyOf<T>]; | ||
/** | ||
@@ -382,3 +388,3 @@ * Gets the enum value for the provided key. | ||
*/ | ||
getValueOrDefault(key: string | null | undefined, defaultValue?: T[keyof T]): T[keyof T] | undefined; | ||
getValueOrDefault(key: string | null | undefined, defaultValue?: T[StringKeyOf<T>]): T[StringKeyOf<T>] | undefined; | ||
/** | ||
@@ -409,3 +415,3 @@ * Gets the enum value for the provided key. | ||
*/ | ||
type Entry<V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> = Readonly<[keyof T, T[keyof T]]>; | ||
type Entry<V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>; | ||
/** | ||
@@ -423,3 +429,3 @@ * A function used in iterating all key/value entries in an enum. | ||
*/ | ||
type Iteratee<R = any, V extends number | string = number | string, T extends EnumLike<V, keyof T> = any> = (this: any, value: T[keyof T], key: keyof T, enumWrapper: EnumWrapper<V, T>, index: number) => R; | ||
type Iteratee<R = any, V extends number | string = number | string, T extends EnumLike<V, StringKeyOf<T>> = any> = (this: any, value: T[StringKeyOf<T>], key: StringKeyOf<T>, enumWrapper: EnumWrapper<V, T>, index: number) => R; | ||
} | ||
@@ -431,3 +437,3 @@ /** | ||
*/ | ||
export declare type NumberEnumWrapper<T extends EnumLike<number, keyof T> = any> = EnumWrapper<number, any>; | ||
export declare type NumberEnumWrapper<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper<number, any>; | ||
export declare namespace NumberEnumWrapper { | ||
@@ -439,3 +445,3 @@ /** | ||
*/ | ||
type Entry<T extends EnumLike<number, keyof T> = any> = EnumWrapper.Entry<number, T>; | ||
type Entry<T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper.Entry<number, T>; | ||
/** | ||
@@ -447,3 +453,3 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only number values. | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<number, keyof T> = any> = EnumWrapper.Iteratee<R, number, T>; | ||
type Iteratee<R = any, T extends EnumLike<number, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, number, T>; | ||
} | ||
@@ -455,3 +461,3 @@ /** | ||
*/ | ||
export declare type StringEnumWrapper<T extends EnumLike<string, keyof T> = any> = EnumWrapper<string, any>; | ||
export declare type StringEnumWrapper<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper<string, any>; | ||
export declare namespace StringEnumWrapper { | ||
@@ -463,3 +469,3 @@ /** | ||
*/ | ||
type Entry<T extends EnumLike<string, keyof T> = any> = EnumWrapper.Entry<string, T>; | ||
type Entry<T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper.Entry<string, T>; | ||
/** | ||
@@ -471,3 +477,3 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains only string values. | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<string, keyof T> = any> = EnumWrapper.Iteratee<R, string, T>; | ||
type Iteratee<R = any, T extends EnumLike<string, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, string, T>; | ||
} | ||
@@ -480,3 +486,3 @@ /** | ||
*/ | ||
export declare type MixedEnumWrapper<T extends EnumLike<number | string, keyof T> = any> = EnumWrapper<number | string, any>; | ||
export declare type MixedEnumWrapper<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper<number | string, any>; | ||
export declare namespace MixedEnumWrapper { | ||
@@ -489,3 +495,3 @@ /** | ||
*/ | ||
type Entry<T extends EnumLike<number | string, keyof T> = any> = EnumWrapper.Entry<number | string, T>; | ||
type Entry<T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Entry<number | string, T>; | ||
/** | ||
@@ -498,3 +504,3 @@ * Type alias for an {@link EnumWrapper.Iteratee} for any type of enum-like object that contains a mix of | ||
*/ | ||
type Iteratee<R = any, T extends EnumLike<number | string, keyof T> = any> = EnumWrapper.Iteratee<R, number | string, T>; | ||
type Iteratee<R = any, T extends EnumLike<number | string, StringKeyOf<T>> = any> = EnumWrapper.Iteratee<R, number | string, T>; | ||
} | ||
@@ -509,1 +515,2 @@ /** | ||
export declare const $enum: typeof EnumWrapper.getCachedInstance; | ||
export {}; |
{ | ||
"name": "ts-enum-util", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "TypeScript Enum Utilities", | ||
@@ -37,5 +37,4 @@ "repository": { | ||
"jest:coverage": "npm run clean:coverage && jest --coverage", | ||
"dtslint:v2_3_plus": "dtslint type_tests/v2_3_plus", | ||
"dtslint:v2_4_plus": "dtslint type_tests/v2_4_plus", | ||
"dtslint": "run-s dtslint:v2_3_plus dtslint:v2_4_plus", | ||
"dtslint:v2_8_plus": "dtslint type_tests/v2_8_plus", | ||
"dtslint": "run-s clean:dist build:types dtslint:v2_8_plus", | ||
"test": "run-s compile prettier:test lint dtslint jest", | ||
@@ -63,6 +62,6 @@ "test:coverage": "run-s compile prettier:test lint dtslint jest:coverage", | ||
"tslint-config-prettier": "1.13.0", | ||
"typescript": "2.8.3" | ||
"typescript": "2.9.1" | ||
}, | ||
"peerDependencies": { | ||
"typescript": ">= 2.3.1 < 2.9" | ||
"typescript": ">= 2.8.1" | ||
}, | ||
@@ -69,0 +68,0 @@ "keywords": [ |
@@ -369,3 +369,3 @@ [![npm version](https://img.shields.io/npm/v/ts-enum-util.svg)](https://www.npmjs.com/package/ts-enum-util) | ||
- _TypeScript 2.3-2.8_: Code that uses `ts-enum-util` will not compile properly with TypeScript versions prior to 2.3. You'll need TypeScript 2.4+ to take advantage of string enums, but you can still use `ts-enum-util` with [Enum-Like Objects](#enum-like-object) containing string values if you are stuck with TypeScript 2.3. Due to a breaking change to `keyof` in TypeScript 2.9, this version of `ts-enum-util` is not compatible with TypeScript 2.9+. Upgrade to `ts-enum-util` v3+ for TypeScript 2.9 compatibility. | ||
- _TypeScript 2.8+_: Due to a breaking change to `keyof` in TypeScript 2.9, this version of `ts-enum-util` is not compatible with TypeScript prior to 2.8. Use v2 of `ts-enum-util` if you require compatibility with earlier TypeScript versions. | ||
- _ES6 Features_: The following ES6 features are used by `ts-enum-util`, so they must exist (either natively or via polyfill) in the run-time environment: | ||
@@ -372,0 +372,0 @@ - `Map` |
143
src/index.ts
@@ -12,2 +12,9 @@ /** | ||
/** | ||
* Extracts only keys of type T that are assignable to type `string`. | ||
* This is necessary starting with TypeScript 2.9 because keyof T can now | ||
* include `number` and `symbol` types. | ||
*/ | ||
type StringKeyOf<T> = Extract<keyof T, string>; | ||
/** | ||
* A generic wrapper for any enum-like object (see {@link EnumLike}). | ||
@@ -27,3 +34,3 @@ * Provides utilities for runtime processing of an enum's values and keys, with strict compile-time | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, keyof T> = any | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> | ||
@@ -49,3 +56,3 @@ implements | ||
*/ | ||
private readonly keysList: ReadonlyArray<keyof T>; | ||
private readonly keysList: ReadonlyArray<StringKeyOf<T>>; | ||
@@ -55,3 +62,3 @@ /** | ||
*/ | ||
private readonly valuesList: ReadonlyArray<T[keyof T]>; | ||
private readonly valuesList: ReadonlyArray<T[StringKeyOf<T>]>; | ||
@@ -65,3 +72,3 @@ /** | ||
*/ | ||
private readonly keysByValueMap: ReadonlyMap<V, keyof T>; | ||
private readonly keysByValueMap: ReadonlyMap<V, StringKeyOf<T>>; | ||
@@ -94,3 +101,3 @@ /** | ||
*/ | ||
public static getCachedInstance<T extends EnumLike<number, keyof T>>( | ||
public static getCachedInstance<T extends EnumLike<number, StringKeyOf<T>>>( | ||
enumObj: T | ||
@@ -106,3 +113,3 @@ ): EnumWrapper<number, T>; | ||
*/ | ||
public static getCachedInstance<T extends EnumLike<string, keyof T>>( | ||
public static getCachedInstance<T extends EnumLike<string, StringKeyOf<T>>>( | ||
enumObj: T | ||
@@ -119,3 +126,3 @@ ): EnumWrapper<string, T>; | ||
public static getCachedInstance< | ||
T extends EnumLike<number | string, keyof T> | ||
T extends EnumLike<number | string, StringKeyOf<T>> | ||
>(enumObj: T): EnumWrapper<number | string, T>; | ||
@@ -150,16 +157,14 @@ /** | ||
private constructor(private readonly enumObj: T) { | ||
this.keysList = Object.freeze( | ||
Object.keys(enumObj) | ||
// Include only keys that are not index keys. | ||
// This is necessary to ignore the reverse-lookup entries that are automatically added | ||
// by TypeScript to numeric enums. | ||
.filter(isNonIndexKey) | ||
// Order of Object.keys() is implementation-dependent, so sort the keys to guarantee | ||
// a consistent order for iteration. | ||
.sort() | ||
); | ||
this.keysList = Object.freeze(Object.keys(enumObj) | ||
// Include only keys that are not index keys. | ||
// This is necessary to ignore the reverse-lookup entries that are automatically added | ||
// by TypeScript to numeric enums. | ||
.filter(isNonIndexKey) | ||
// Order of Object.keys() is implementation-dependent, so sort the keys to guarantee | ||
// a consistent order for iteration. | ||
.sort() as StringKeyOf<T>[]); | ||
const length = this.keysList.length; | ||
const valuesList = new Array<T[keyof T]>(length); | ||
const keysByValueMap = new Map<V, keyof T>(); | ||
const valuesList = new Array<T[StringKeyOf<T>]>(length); | ||
const keysByValueMap = new Map<V, StringKeyOf<T>>(); | ||
@@ -201,3 +206,3 @@ // According to multiple tests found on jsperf.com, a plain for loop is faster than using | ||
*/ | ||
public keys(): IterableIterator<keyof T> { | ||
public keys(): IterableIterator<StringKeyOf<T>> { | ||
let index = 0; | ||
@@ -208,3 +213,3 @@ | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<keyof T> = { | ||
const result: IteratorResult<StringKeyOf<T>> = { | ||
done: isDone, | ||
@@ -221,3 +226,3 @@ // "as any" cast is necessary to work around this bug: | ||
[Symbol.iterator](): IterableIterator<keyof T> { | ||
[Symbol.iterator](): IterableIterator<StringKeyOf<T>> { | ||
return this; | ||
@@ -236,3 +241,3 @@ } | ||
*/ | ||
public values(): IterableIterator<T[keyof T]> { | ||
public values(): IterableIterator<T[StringKeyOf<T>]> { | ||
let index = 0; | ||
@@ -243,3 +248,3 @@ | ||
const isDone = index >= this.length; | ||
const result: IteratorResult<T[keyof T]> = { | ||
const result: IteratorResult<T[StringKeyOf<T>]> = { | ||
done: isDone, | ||
@@ -256,3 +261,3 @@ // "as any" cast is necessary to work around this bug: | ||
[Symbol.iterator](): IterableIterator<T[keyof T]> { | ||
[Symbol.iterator](): IterableIterator<T[StringKeyOf<T>]> { | ||
return this; | ||
@@ -359,3 +364,3 @@ } | ||
*/ | ||
public getKeys(): (keyof T)[] { | ||
public getKeys(): (StringKeyOf<T>)[] { | ||
// need to return a copy of this.keysList so it can be returned as Array instead of ReadonlyArray. | ||
@@ -372,3 +377,3 @@ return this.keysList.slice(); | ||
*/ | ||
public getValues(): T[keyof T][] { | ||
public getValues(): T[StringKeyOf<T>][] { | ||
// need to return a copy of this.valuesList so it can be returned as Array instead of ReadonlyArray. | ||
@@ -395,3 +400,3 @@ return this.valuesList.slice(); | ||
*/ | ||
public isKey(key: string | null | undefined): key is keyof T { | ||
public isKey(key: string | null | undefined): key is StringKeyOf<T> { | ||
return ( | ||
@@ -411,3 +416,3 @@ key != null && | ||
*/ | ||
public asKeyOrThrow(key: string | null | undefined): keyof T { | ||
public asKeyOrThrow(key: string | null | undefined): StringKeyOf<T> { | ||
if (this.isKey(key)) { | ||
@@ -432,4 +437,4 @@ return key; | ||
key: string | null | undefined, | ||
defaultKey: keyof T | ||
): keyof T; | ||
defaultKey: StringKeyOf<T> | ||
): StringKeyOf<T>; | ||
/** | ||
@@ -445,4 +450,4 @@ * Casts a string to a properly-typed key for this enum. | ||
key: string | null | undefined, | ||
defaultKey?: keyof T | ||
): keyof T | undefined; | ||
defaultKey?: StringKeyOf<T> | ||
): StringKeyOf<T> | undefined; | ||
/** | ||
@@ -482,3 +487,3 @@ * Casts a string to a properly-typed key for this enum. | ||
key: string | null | undefined, | ||
defaultKey?: keyof T | string | ||
defaultKey?: StringKeyOf<T> | string | ||
): string | undefined { | ||
@@ -488,3 +493,3 @@ if (this.isKey(key)) { | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return key as keyof T; | ||
return key as StringKeyOf<T>; | ||
} else { | ||
@@ -501,3 +506,3 @@ return defaultKey; | ||
*/ | ||
public isValue(value: V | null | undefined): value is T[keyof T] { | ||
public isValue(value: V | null | undefined): value is T[StringKeyOf<T>] { | ||
return value != null && this.keysByValueMap.has(value); | ||
@@ -513,3 +518,3 @@ } | ||
*/ | ||
public asValueOrThrow(value: V | null | undefined): T[keyof T] { | ||
public asValueOrThrow(value: V | null | undefined): T[StringKeyOf<T>] { | ||
if (this.isValue(value)) { | ||
@@ -534,4 +539,4 @@ return value; | ||
value: V | null | undefined, | ||
defaultValue: T[keyof T] | ||
): T[keyof T]; | ||
defaultValue: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>]; | ||
/** | ||
@@ -547,4 +552,4 @@ * Casts a value to a properly-typed value for this enum. | ||
value: V | null | undefined, | ||
defaultValue?: T[keyof T] | ||
): T[keyof T] | undefined; | ||
defaultValue?: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>] | undefined; | ||
/** | ||
@@ -581,3 +586,3 @@ * Casts a value to a properly-typed value for this enum. | ||
value: V | null | undefined, | ||
defaultValue?: T[keyof T] | V | ||
defaultValue?: T[StringKeyOf<T>] | V | ||
): V | undefined { | ||
@@ -600,3 +605,3 @@ if (this.isValue(value)) { | ||
*/ | ||
public getKeyOrThrow(value: V | null | undefined): keyof T { | ||
public getKeyOrThrow(value: V | null | undefined): StringKeyOf<T> { | ||
// NOTE: Intentionally not using isValue() or asValueOrThrow() to avoid making two key lookups into the map | ||
@@ -628,4 +633,4 @@ // for successful lookups. | ||
value: V | null | undefined, | ||
defaultKey: keyof T | ||
): keyof T; | ||
defaultKey: StringKeyOf<T> | ||
): StringKeyOf<T>; | ||
/** | ||
@@ -643,4 +648,4 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
value: V | null | undefined, | ||
defaultKey?: keyof T | ||
): keyof T | undefined; | ||
defaultKey?: StringKeyOf<T> | ||
): StringKeyOf<T> | undefined; | ||
/** | ||
@@ -686,3 +691,3 @@ * Performs a reverse lookup from enum value to corresponding enum key. | ||
value: V | null | undefined, | ||
defaultKey?: keyof T | string | ||
defaultKey?: StringKeyOf<T> | string | ||
): string | undefined { | ||
@@ -707,3 +712,3 @@ // NOTE: Intentionally not using isValue() to avoid making two key lookups into the map for successful lookups. | ||
*/ | ||
public getValueOrThrow(key: string | null | undefined): T[keyof T] { | ||
public getValueOrThrow(key: string | null | undefined): T[StringKeyOf<T>] { | ||
// NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
@@ -725,4 +730,4 @@ // lookups for keys that match properties on Object.prototype, or keys that match the index keys of | ||
key: string | null | undefined, | ||
defaultValue: T[keyof T] | ||
): T[keyof T]; | ||
defaultValue: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>]; | ||
/** | ||
@@ -738,4 +743,4 @@ * Gets the enum value for the provided key. | ||
key: string | null | undefined, | ||
defaultValue?: T[keyof T] | ||
): T[keyof T] | undefined; | ||
defaultValue?: T[StringKeyOf<T>] | ||
): T[StringKeyOf<T>] | undefined; | ||
/** | ||
@@ -775,3 +780,3 @@ * Gets the enum value for the provided key. | ||
key: string | null | undefined, | ||
defaultValue?: T[keyof T] | V | ||
defaultValue?: T[StringKeyOf<T>] | V | ||
): V | undefined { | ||
@@ -784,3 +789,3 @@ // NOTE: The key MUST be separately validated before looking up the entry in enumObj to avoid false positive | ||
// https://github.com/Microsoft/TypeScript/issues/21950 | ||
return this.enumObj[key as keyof T]; | ||
return this.enumObj[key as StringKeyOf<T>]; | ||
} else { | ||
@@ -800,4 +805,4 @@ return defaultValue; | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, keyof T> = any | ||
> = Readonly<[keyof T, T[keyof T]]>; | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> = Readonly<[StringKeyOf<T>, T[StringKeyOf<T>]]>; | ||
@@ -819,7 +824,7 @@ /** | ||
V extends number | string = number | string, | ||
T extends EnumLike<V, keyof T> = any | ||
T extends EnumLike<V, StringKeyOf<T>> = any | ||
> = ( | ||
this: any, | ||
value: T[keyof T], | ||
key: keyof T, | ||
value: T[StringKeyOf<T>], | ||
key: StringKeyOf<T>, | ||
enumWrapper: EnumWrapper<V, T>, | ||
@@ -836,3 +841,3 @@ index: number | ||
export type NumberEnumWrapper< | ||
T extends EnumLike<number, keyof T> = any | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper<number, any>; | ||
@@ -847,3 +852,3 @@ | ||
export type Entry< | ||
T extends EnumLike<number, keyof T> = any | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<number, T>; | ||
@@ -859,3 +864,3 @@ | ||
R = any, | ||
T extends EnumLike<number, keyof T> = any | ||
T extends EnumLike<number, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, number, T>; | ||
@@ -870,3 +875,3 @@ } | ||
export type StringEnumWrapper< | ||
T extends EnumLike<string, keyof T> = any | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper<string, any>; | ||
@@ -881,3 +886,3 @@ | ||
export type Entry< | ||
T extends EnumLike<string, keyof T> = any | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<string, T>; | ||
@@ -893,3 +898,3 @@ | ||
R = any, | ||
T extends EnumLike<string, keyof T> = any | ||
T extends EnumLike<string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, string, T>; | ||
@@ -905,3 +910,3 @@ } | ||
export type MixedEnumWrapper< | ||
T extends EnumLike<number | string, keyof T> = any | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper<number | string, any>; | ||
@@ -917,3 +922,3 @@ | ||
export type Entry< | ||
T extends EnumLike<number | string, keyof T> = any | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Entry<number | string, T>; | ||
@@ -930,3 +935,3 @@ | ||
R = any, | ||
T extends EnumLike<number | string, keyof T> = any | ||
T extends EnumLike<number | string, StringKeyOf<T>> = any | ||
> = EnumWrapper.Iteratee<R, number | string, T>; | ||
@@ -933,0 +938,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150911
2181