Skip to content

Commit

Permalink
Added types
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Apr 18, 2024
1 parent 0e3d367 commit 58ce9e0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './misc.js';
export * from './numbers.js';
export * from './objects.js';
export * from './random.js';
export * from './types.js';
9 changes: 7 additions & 2 deletions src/objects.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export const filterObject = (object: object, ...keys: string[]) => Object.fromEntries(Object.entries(object).filter(([key]) => keys.includes(key)));
export function filterObject<T extends object, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K> {
const entries = <[K, T[K]][]>Object.entries(object);
return <Omit<T, K>>(<unknown>Object.fromEntries(entries.filter(([key]) => keys.includes(key))));
}

import type * as FS from 'fs';

let _fs: typeof FS;
try {
_fs = await import('fs');
} catch (e) {}
} catch (e) {
_fs = null;
}

export function isJSON(str: string) {
try {
Expand Down
76 changes: 76 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Expands the type T (for intellisense and debugging)
* @see https://stackoverflow.com/a/69288824/17637456
*/
export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: Expand<A>) => Expand<R> : T extends infer O ? { [K in keyof O]: O[K] } : never;

/**
* Recursivly expands the type T (for intellisense and debugging)
* @see https://stackoverflow.com/a/69288824/17637456
*/
export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
: T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T;

/**
* Extracts an object with properties assignable to P from an object T
* @see https://stackoverflow.com/a/71532723/17637456
*/
export type ExtractProperties<T, P> = {
[K in keyof T as T[K] extends infer Prop ? (Prop extends P ? K : never) : never]: T[K];
};

/**
* Extract the keys of T which are required
* @see https://stackoverflow.com/a/55247867/17637456
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K }[keyof T];

/**
* @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
*/
export type RequiredProperties<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>;

/**
* @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
*/
export type DeepRequired<T> = {
[K in keyof T]: Required<DeepRequired<T[K]>>;
};

/**
* @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
*/
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;

/**
* @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
*/
export type NestedKeys<T extends object> = {
[P in keyof T & (string | number)]: T[P] extends Date ? `${P}` : T[P] extends Record<string, unknown> ? `${P}` | `${P}.${NestedKeys<T[P]>}` : `${P}`;
}[keyof T & (string | number)];

/**
* @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
*/
export type PartialRecursive<T> = {
[P in keyof T]?: T[P] extends (infer U)[] ? PartialRecursive<U>[] : T[P] extends object | undefined ? PartialRecursive<T[P]> : T[P];
};

/**
* Get the keys of a union of objects
* @see https://stackoverflow.com/a/65805753/17637456
*/
export type UnionKeys<T> = T extends T ? keyof T : never;

type StrictUnionHelper<T, TAll> = T extends unknown ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;

/**
* @see https://stackoverflow.com/a/65805753/17637456
*/
export type StrictUnion<T> = Expand<StrictUnionHelper<T, T>>;

0 comments on commit 58ce9e0

Please sign in to comment.