Created
May 22, 2021 16:27
-
-
Save sshmaxime/64d900c911de6718d4415d1c7df99c80 to your computer and use it in GitHub Desktop.
🔬 Typescript cheatsheet
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
// 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