Last active
March 2, 2021 03:41
-
-
Save ackvf/21e976cf4970841482e2b14870549f63 to your computer and use it in GitHub Desktop.
React TypeScript types
This file contains 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 guards */ | |
export function assertType<T>(obj: any, assertion: (obj: T) => boolean): asserts obj is T { | |
if (!assertion(obj)) throw new Error('Invalid type') | |
} | |
export function is<T>(obj: any, assertion: (obj: T) => boolean): obj is T { | |
return assertion(obj); | |
} | |
function isFish(pet: Fish | Bird): pet is Fish { | |
return (<Fish>pet).swim !== undefined; | |
} | |
/* | |
Assertion functions | |
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions | |
*/ | |
/* Module augmentation */ | |
// declare module 'React' { | |
// interface Component<P, S> { | |
// setState<K extends keyof T, T = this['state']>( | |
// state: ((prevState: Readonly<T>, props: Readonly<P>) => (Pick<T, K> | T | null)) | (Pick<T, K> | T | null), | |
// callback?: () => void | |
// ): void | |
// } | |
// } | |
declare module 'react' { | |
interface Component<P = {}> { | |
setState<K extends keyof T, T extends this['state']>( | |
state: ((prevState: Readonly<T>, props: Readonly<P>) => Pick<T, K> | T | null) | (Pick<T, K> | T | null), | |
callback?: () => void | |
): void; | |
} | |
interface PureComponent<P = {}> { | |
setState<K extends keyof T, T extends this['state']>( | |
state: ((prevState: Readonly<T>, props: Readonly<P>) => Pick<T, K> | T | null) | (Pick<T, K> | T | null), | |
callback?: () => void | |
): void; | |
} | |
} | |
declare global { | |
interface Console { | |
debug(message?: any, ...optionalParams: any[]): false; | |
error(message?: any, ...optionalParams: any[]): false; | |
warn(message?: any, ...optionalParams: any[]): false; | |
log(message?: any, ...optionalParams: any[]): false; | |
} | |
} |
This file contains 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
/* Ambient module declaration */ | |
declare module JSX { | |
interface IntrinsicElements { | |
'ion-icon': {name: string} | |
} | |
} | |
declare module '*.svg'; | |
declare module '*.png'; | |
declare module '*.jpg'; | |
type CoercePartial<T extends {}, U> = { | |
[K in keyof T]?: T[K] extends Array<infer A> | |
? Array<CoercePartial<A, U>> | |
: T[K] extends object | |
? CoercePartial<T[K], U> | |
: U; | |
}; | |
type Modify<T, R> = Omit<T, keyof R> & R; | |
type NonNullableDeep<T> = { | |
[K in keyof T]-?: NonNullableDeep<NonNullable<T[K]>>; | |
}; | |
type RequiredDeep<T> = NonNullableDeep<T>; | |
type RequiredDeep_old<T extends {}> = { | |
[K in keyof T]-?: NonNullable<T[K]> extends Array<infer U> | |
? NonNullable<RequiredDeep<U>>[] | |
: NonNullable<T[K]> extends object | |
? RequiredDeep<NonNullable<T[K]>> | |
: NonNullable<T[K]> | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment