Last active
October 22, 2023 11:57
-
-
Save wmakeev/1dbd25dc282481db9767eb70b303f4d4 to your computer and use it in GitHub Desktop.
[TypeScript type fixes] #typescript #fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** @type {TypedEntries} */ | |
| export function typedEntries(obj) { | |
| // @ts-expect-error ignore | |
| return Object.entries(obj); | |
| } | |
| /** @type {TypedFromEntries} */ | |
| export function typedFromEntries(entries) { | |
| return Object.fromEntries(entries); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //#region https://dev.to/harry0000/a-bit-convenient-typescript-type-definitions-for-objectentries-d6g | |
| // prettier-ignore | |
| type TupleEntry<T extends readonly unknown[], I extends unknown[] = [], R = never> = | |
| T extends readonly [infer Head, ...infer Tail] ? | |
| TupleEntry<Tail, [...I, unknown], R | [`${I['length']}`, Head]> : | |
| R | |
| // prettier-ignore | |
| // eslint-disable-next-line @typescript-eslint/ban-types | |
| type ObjectEntry<T extends {}> = | |
| // eslint-disable-next-line @typescript-eslint/ban-types | |
| T extends object ? | |
| { [K in keyof T]: [K, Required<T>[K]] }[keyof T] extends infer E ? | |
| E extends [infer K, infer V] ? | |
| K extends string | number ? | |
| [`${K}`, V] : | |
| never : | |
| never : | |
| never : | |
| never | |
| // prettier-ignore | |
| // eslint-disable-next-line @typescript-eslint/ban-types | |
| type Entry<T extends {}> = | |
| T extends readonly [unknown, ...unknown[]] ? | |
| TupleEntry<T> : | |
| T extends ReadonlyArray<infer U> ? | |
| [`${number}`, U] : | |
| ObjectEntry<T> | |
| // eslint-disable-next-line @typescript-eslint/ban-types | |
| type TypedEntries = <T extends {}>(object: T) => ReadonlyArray<Entry<T>>; | |
| /** | |
| * Returns an object created by key-value entries for properties and methods | |
| * @param entries An iterable object that contains key-value entries for properties and methods. | |
| */ | |
| type TypedFromEntries = <T = any, P>( | |
| entries: Iterable<readonly [P, T]> | |
| ) => { [k in P]: T }; | |
| //#endregion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** @type {TypedHasOwn} */ | |
| export function typedHasOwn(obj, fieldName) { | |
| return Object.hasOwn(obj, fieldName) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type TypedHasOwn = <Obj extends object>( | |
| obj: Obj, | |
| field: any | |
| ) => field is keyof Obj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** @type {TypedKeys} */ | |
| export function typedKeys(obj) { | |
| // @ts-expect-error ignore | |
| return Object.keys(obj) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type TypedKeys = <T = any>(obj: T) => (keyof T)[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment