@battis/typescript-tricks 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/EnumeratedTypes.d.ts +11 -0
- package/dist/EnumeratedTypes.js +12 -0
- package/dist/PropertyRequirements.d.ts +24 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type EnumObject = {
|
|
2
|
+
[key: string]: number | string;
|
|
3
|
+
};
|
|
4
|
+
type EnumObjectEnum<E extends EnumObject> = E extends {
|
|
5
|
+
[key: string]: infer ET | string;
|
|
6
|
+
} ? ET : never;
|
|
7
|
+
/**
|
|
8
|
+
* @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
|
|
9
|
+
*/
|
|
10
|
+
export declare function getEnumValues<E extends EnumObject>(enumObject: E): EnumObjectEnum<E>[];
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEnumValues = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
|
|
6
|
+
*/
|
|
7
|
+
function getEnumValues(enumObject) {
|
|
8
|
+
return Object.keys(enumObject)
|
|
9
|
+
.filter((key) => Number.isNaN(Number(key)))
|
|
10
|
+
.map((key) => enumObject[key]);
|
|
11
|
+
}
|
|
12
|
+
exports.getEnumValues = getEnumValues;
|
|
@@ -14,3 +14,27 @@ export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<
|
|
|
14
14
|
export type RecursivePartial<T> = {
|
|
15
15
|
[P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* ```ts
|
|
19
|
+
* type A = {
|
|
20
|
+
* d: number,
|
|
21
|
+
* e: string
|
|
22
|
+
* f: boolean
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* type B = ReplaceProperty<A, 'e', number[]>;
|
|
26
|
+
* // type B = {
|
|
27
|
+
* // d: number,
|
|
28
|
+
* // e: number[],
|
|
29
|
+
* // f: boolean
|
|
30
|
+
* // }
|
|
31
|
+
* ```
|
|
32
|
+
* @see {@link https://stackoverflow.com/a/51599774 StackOverflow response}
|
|
33
|
+
*/
|
|
34
|
+
export type ReplaceProperty<T, K extends keyof T, TReplace> = Identity<Pick<T, Exclude<keyof T, K>> & {
|
|
35
|
+
[P in K]: TReplace;
|
|
36
|
+
}>;
|
|
37
|
+
type Identity<T> = {
|
|
38
|
+
[P in keyof T]: T[P];
|
|
39
|
+
};
|
|
40
|
+
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./Constructor"), exports);
|
|
18
|
+
__exportStar(require("./EnumeratedTypes"), exports);
|
|
18
19
|
__exportStar(require("./Mixin"), exports);
|
|
19
20
|
__exportStar(require("./PropertyRequirements"), exports);
|
|
20
21
|
__exportStar(require("./Shorthand"), exports);
|
package/package.json
CHANGED