Files
discordeno/packages/bot/src/optionalize.ts
Fleny f0ac958903 chore(website): Cleanup docusaurus setup (#3355)
* Update docusaurus typescript setup for v3

And fix lint-staged and eslint

* Enable automatic JSX runtime

* Remove babel config and dependencies

* update yarn.lock

* add typecheck to site workflow

* update typedoc config

* downgrade docusaurus packages

* Update site.yml

* Type context and options in webpack-docusaurus-plugin.ts

---------

Co-authored-by: Matt Hatcher <3768988+MatthewSH@users.noreply.github.com>
2024-02-09 20:34:59 +00:00

74 lines
1.7 KiB
TypeScript

export type OptionalizeAux<T extends object> = Id<
{
[K in KeysWithUndefined<T>]?: Optionalize<T[K]>
} & {
[K in Exclude<keyof T, KeysWithUndefined<T>>]: T[K] extends ObjectLiteral ? Optionalize<T[K]> : T[K]
}
>
/**
* Makes all of properties in T optional when they're null | undefined
* it is recursive
*/
export type Optionalize<T> = T extends object
? T extends unknown[]
? number extends T['length']
? T[number] extends object
? Array<OptionalizeAux<T[number]>>
: T
: Partial<T>
: OptionalizeAux<T>
: T
export type KeysWithUndefined<T> = {
[K in keyof T]-?: undefined extends T[K] ? K : null extends T[K] ? K : never
}[keyof T]
/**
* alternative to 'object' or '{}'
* @example:
* export const o: ObjectLiteral = [] as object; // error
* export const o: object = []; // no error
*/
export type ObjectLiteral<T = unknown> = {
[K in PropertyKey]: T
}
/**
* object identity type
*/
export type Id<T> = T extends infer U
? {
[K in keyof U]: U[K]
}
: never
/** Array with no utilty methods, aka Object.create(null) */
export interface ArrayWithNoPrototype<T> {
[index: number]: T | ArrayWithNoPrototype<T>
}
/**
* Allows any type but T
* it is recursive
* @example
* export type RequestData = Record<string, AnythingBut<bigint>>;
*/
export type AnythingBut<T> = Exclude<
| Primitive
| {
[K in PropertyKey]: AnythingBut<T>
}
| ArrayWithNoPrototype<
| Primitive
| {
[K in PropertyKey]: AnythingBut<T>
}
>,
T
>
/** Non object primitives */
export type Primitive = string | number | symbol | bigint | boolean | undefined | null
// | object <- don't make object a primitive