@sheerid/jslib-nightly
Version:
SheerID JavaScript Library
47 lines (43 loc) • 1.48 kB
TypeScript
/**
* @description A debug type that will provide an expanded type description when
* reviewing complex types with a lot of inheritance and/or intersections
* @private
* Example:
type A = { foo: string }
function bar(newObject: PropertiesOf<A>) {...}
bar({ foo: 'baz' }) // valid
bar({ otherProp: 'stuff' }) // invalid
*/
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
/**
* @description returns a string union that facilitates typeahead while allowing
* any string to be passed in. e.g. API strings that might expand over time
*/
export type LooseAutoComplete<T extends string> = T | (string & {});
/**
* @description Infers a union type from the values of an object, useful for enum-like consts
* @private
* Example:
const Obj = {
FOO: "foo",
BAR: "bar",
BAZ: []
}
export type Values = StringValueOf<typeof Obj>; // "foo" | "bar"
*/
export type StringValueOf<T> = {
[K in keyof T]: T[K] extends string ? T[K] : never;
}[keyof T];
/**
* @description Recursively applies Partial to a type
* @private
*/
export type DeepPartial<Thing> = Thing extends Function ? Thing : Thing extends Array<infer InferredArrayMember> ? DeepPartialArray<InferredArrayMember> : Thing extends object ? DeepPartialObject<Thing> : Thing | undefined;
interface DeepPartialArray<Thing> extends Array<DeepPartial<Thing>> {
}
type DeepPartialObject<Thing> = {
[Key in keyof Thing]?: DeepPartial<Thing[Key]>;
};
export {};