Skip to content

Instantly share code, notes, and snippets.

@sshmaxime
Created May 22, 2021 16:27
Show Gist options
  • Save sshmaxime/64d900c911de6718d4415d1c7df99c80 to your computer and use it in GitHub Desktop.
Save sshmaxime/64d900c911de6718d4415d1c7df99c80 to your computer and use it in GitHub Desktop.
🔬 Typescript cheatsheet
// Change last param of a function
type LastIndex<T extends readonly any[]> = ((...t: T) => void) extends (x: any, ...r: infer R) => void
? Exclude<keyof T, keyof R>
: never;
type ReplaceLastParam<TParams extends readonly any[], TReplace> = {
[K in keyof TParams]: K extends LastIndex<TParams> ? TReplace : TParams[K];
};
type ReplaceLast<F, TReplace> = F extends (...args: infer T) => infer R
? (...args: ReplaceLastParam<T, TReplace>) => R
: never;
type ReplacedArg = { param: string };
type OriginFunc = typeof myFunction;
type NewFunc = ReplaceLast<OriginFunc, ReplacedArg>;
////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment