Skip to content

Commit

Permalink
chore: arrange typescript types in project
Browse files Browse the repository at this point in the history
Reference-to: #49, #51
Signed-off-by: Prince Muel <[email protected]>
  • Loading branch information
princemuel committed May 30, 2023
1 parent 3d47b98 commit 5c6758c
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 228 deletions.
27 changes: 27 additions & 0 deletions src/@types/defaults.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface IconProps extends React.ComponentPropsWithoutRef<'svg'> {}

type $ElementProps<E extends React.ElementType<any>> = {
children: React.ReactNode;
as?: E;
};

type ElementProps<E extends React.ElementType<any>> = $ElementProps<E> &
Omit<React.ComponentPropsWithoutRef<E>, keyof $ElementProps<E>>;

interface IconProps extends React.ComponentPropsWithoutRef<'svg'> {}

type PropsFrom<T> = T extends React.FC<infer Props>
? Props
: T extends React.Component<infer Props>
? Props
: T extends object
? { [K in keyof T]: T[K] }
: never;

/*==============================*
EVENT TYPES
==============================*/
type ReactFormEvent = React.FormEvent<HTMLFormElement>;
type ReactSelectEvent = React.MouseEvent<HTMLLIElement>;
type ReactInputEvent = React.ChangeEvent<HTMLInputElement>;
type ReactMouseEvent = React.MouseEvent<HTMLButtonElement>;
18 changes: 18 additions & 0 deletions src/@types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export declare global {
interface GlobalReducerActions {}
}

type GlobalReducer<IState> = (
state: IState,
action: {
[ActionType in keyof GlobalReducerActions]: {
type: ActionType;
} & GlobalReducerActions[ActionType];
}[keyof GlobalReducerActions]
) => IState;

export declare module 'react' {
function forwardRef<T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
}
95 changes: 95 additions & 0 deletions src/@types/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*===============================*
HELPER TYPES
*===============================*
*/

type Immutable<T> = T extends PrimitiveType
? T
: T extends AtomicObject
? T
: T extends Array<infer A>
? ReadonlyArray<Immutable<A>>
: T extends IfAvailable<ReadonlyMap<infer K, infer V>>
? ReadonlyMap<Immutable<K>, Immutable<V>>
: T extends IfAvailable<ReadonlySet<infer S>>
? ReadonlySet<Immutable<S>>
: T extends WeakReferences
? T
: T extends object
? {
// !NOTE: removes methods on object
readonly [P in NonFunctionPropertyNames<T>]: Immutable<T[P]>;
// !NOTE: use { readonly [P in keyof T]: Immutable<T[P]> } to add methods
}
: unknown;

type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
}[keyof T];

type LooseAutocomplete<T extends string> = T | Omit<string, T>;

type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends object
? T extends infer O
? { [K in keyof O]: Expand<O[K]> }
: never
: T;

type DeepPartial<T> = T extends Function
? T
: T extends Array<infer A>
? DeepPartialArray<A>
: T extends object
? DeepPartialObject<T>
: T | undefined;

type DeepPartialArray<T> = Array<DeepPartial<T>>;
type DeepPartialObject<T> = {
[K in keyof T]?: DeepPartial<T[K]>;
};

type KeyValuePair<K extends keyof any = string, V = string> = Record<K, V>;
interface RecursiveKeyValuePair<K extends keyof any = string, V = string> {
[key: string]: V | RecursiveKeyValuePair<K, V>;
}

type OptionalUnion<
U extends Record<string, any>,
A extends keyof U = U extends U ? keyof U : never
> = U extends unknown ? { [P in Exclude<A, keyof U>]?: never } & U : never;

type WeakReferences =
| IfAvailable<WeakMap<any, any>>
| IfAvailable<WeakSet<any>>;

type IfAvailable<T, Fallback = void> = true | false extends (
T extends never ? true : false
)
? Fallback
: keyof T extends never
? Fallback
: T;

// get method names in an object
type FunctionPropertyNames<T> = {
[P in keyof T]: T[P] extends Function ? P : never;
}[keyof T];
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;

// get non method names in an object
type NonFunctionPropertyNames<T> = {
[P in keyof T]: T[P] extends Function ? never : P;
}[keyof T];
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;

type PrimitiveType =
| string
| number
| boolean
| undefined
| null
| bigint
| symbol;
type AtomicObject = Function | RegExp | Promise<any> | Date;
228 changes: 0 additions & 228 deletions src/@types/index.d.ts

This file was deleted.

Loading

0 comments on commit 5c6758c

Please sign in to comment.