@xylabs/object
Advanced tools
/** | ||
* A utility type that, given a `Record<string, unknown>`, returns a readonly version | ||
* of that record. This results in a type where all properties of `T` are readonly. | ||
* | ||
* @template T - The record type to make readonly. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Given a record: | ||
* const MyRecord = { | ||
* FOO: "foo", | ||
* BAR: "bar" | ||
* } as const; | ||
* | ||
* // Applying Enum<T> results in: | ||
* type MyRecordEnum = Enum<typeof MyRecord>; | ||
* // { | ||
* // readonly FOO: "foo"; | ||
* // readonly BAR: "bar"; | ||
* // } | ||
* ``` | ||
*/ | ||
export type Enum<T extends Record<string, unknown>> = { | ||
readonly [K in keyof T]: T[K]; | ||
}; | ||
/** | ||
* Transforms a given record object into a readonly "enum-like" structure while preserving | ||
@@ -71,9 +46,40 @@ * the literal types of its values. This allows you to use the returned object both at runtime | ||
* // } | ||
* ``` | ||
*/ | ||
export declare const Enum: <const T extends Record<string | number | symbol, unknown>>(obj: Readonly<T>) => Enum<T>; | ||
/** | ||
* A utility type that, given a `Record<string, unknown>`, returns a readonly version | ||
* of that record. This results in a type where all properties of `T` are readonly. | ||
* | ||
* // You can then derive a type for the values: | ||
* type DnsRecordTypeValue = typeof DnsRecordType[keyof typeof DnsRecordType]; | ||
* // DnsRecordTypeValue = 1 | 28 | 257 | 5 | 39 | 15 | 2 | 12 | 6 | 99 | 33 | 16 | ||
* @template T - The record type to make readonly. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Given a record: | ||
* export const DnsRecordType = Enum({ | ||
* A: 1, | ||
* AAAA: 28, | ||
* CAA: 257, | ||
* CNAME: 5, | ||
* DNAME: 39, | ||
* MX: 15, | ||
* NS: 2, | ||
* PTR: 12, | ||
* SOA: 6, | ||
* SPF: 99, | ||
* SRV: 33, | ||
* TXT: 16, | ||
* }) | ||
* | ||
* // Now the type inference will preserve the literal types: | ||
* export type DnsRecordType = Enum<typeof DnsRecordType> | ||
* ``` | ||
*/ | ||
export declare const Enum: <const T extends Record<string, unknown>>(obj: Readonly<T>) => Enum<T>; | ||
export type Enum<T extends Readonly<Record<string | number | symbol, unknown>>> = { | ||
readonly [K in keyof T]: T[K]; | ||
}; | ||
/** | ||
* A utility type that, given an `Enum` object, returns the union of its values. | ||
*/ | ||
export type EnumValue<T extends Record<string | number | symbol, unknown>, K = Enum<T>> = K[keyof K]; | ||
//# sourceMappingURL=Enum.d.ts.map |
{ | ||
"name": "@xylabs/object", | ||
"version": "4.4.22", | ||
"version": "4.4.23", | ||
"description": "Primary SDK for using XYO Protocol 2.0", | ||
@@ -32,6 +32,6 @@ "homepage": "https://xyo.network", | ||
"dependencies": { | ||
"@xylabs/assert": "^4.4.22", | ||
"@xylabs/logger": "^4.4.22", | ||
"@xylabs/promise": "^4.4.22", | ||
"@xylabs/typeof": "^4.4.22" | ||
"@xylabs/assert": "^4.4.23", | ||
"@xylabs/logger": "^4.4.23", | ||
"@xylabs/promise": "^4.4.23", | ||
"@xylabs/typeof": "^4.4.23" | ||
}, | ||
@@ -38,0 +38,0 @@ "devDependencies": { |
/** | ||
* A utility type that, given a `Record<string, unknown>`, returns a readonly version | ||
* of that record. This results in a type where all properties of `T` are readonly. | ||
* | ||
* @template T - The record type to make readonly. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Given a record: | ||
* const MyRecord = { | ||
* FOO: "foo", | ||
* BAR: "bar" | ||
* } as const; | ||
* | ||
* // Applying Enum<T> results in: | ||
* type MyRecordEnum = Enum<typeof MyRecord>; | ||
* // { | ||
* // readonly FOO: "foo"; | ||
* // readonly BAR: "bar"; | ||
* // } | ||
* ``` | ||
*/ | ||
export type Enum<T extends Record<string, unknown>> = { | ||
readonly [K in keyof T]: T[K] | ||
} | ||
/** | ||
* Transforms a given record object into a readonly "enum-like" structure while preserving | ||
@@ -72,10 +46,43 @@ * the literal types of its values. This allows you to use the returned object both at runtime | ||
* // } | ||
* | ||
* // You can then derive a type for the values: | ||
* type DnsRecordTypeValue = typeof DnsRecordType[keyof typeof DnsRecordType]; | ||
* // DnsRecordTypeValue = 1 | 28 | 257 | 5 | 39 | 15 | 2 | 12 | 6 | 99 | 33 | 16 | ||
* ``` | ||
*/ | ||
export const Enum = <const T extends Record<string, unknown>>(obj: Readonly<T>): Enum<T> => { | ||
export const Enum = <const T extends Record<string | number | symbol, unknown>>(obj: Readonly<T>): Enum<T> => { | ||
return obj | ||
} | ||
/** | ||
* A utility type that, given a `Record<string, unknown>`, returns a readonly version | ||
* of that record. This results in a type where all properties of `T` are readonly. | ||
* | ||
* @template T - The record type to make readonly. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Given a record: | ||
* export const DnsRecordType = Enum({ | ||
* A: 1, | ||
* AAAA: 28, | ||
* CAA: 257, | ||
* CNAME: 5, | ||
* DNAME: 39, | ||
* MX: 15, | ||
* NS: 2, | ||
* PTR: 12, | ||
* SOA: 6, | ||
* SPF: 99, | ||
* SRV: 33, | ||
* TXT: 16, | ||
* }) | ||
* | ||
* // Now the type inference will preserve the literal types: | ||
* export type DnsRecordType = Enum<typeof DnsRecordType> | ||
* ``` | ||
*/ | ||
export type Enum<T extends Readonly<Record<string | number | symbol, unknown>>> = { | ||
readonly [K in keyof T]: T[K] | ||
} | ||
/** | ||
* A utility type that, given an `Enum` object, returns the union of its values. | ||
*/ | ||
export type EnumValue<T extends Record<string | number | symbol, unknown>, K = Enum<T>> = K[keyof K] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
137009
0.45%1318
0.92%