Created
April 17, 2020 05:49
-
-
Save GheorgheP/63b732ea02f4ed7116465fb5d124ea8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Null = undefined | null | |
type MVal<T> = T | Null | |
type Nullify<T> = { | |
[P in keyof T]: MVal<T[P]>; | |
}; | |
type MParams<T extends (...args: any) => any> = T extends (...args: infer P) => any ? Nullify<P> : never; | |
declare function apply<F extends (...args: any) => any>(f:F, ...args: Parameters<F>): ReturnType<F> | |
declare function mApply<F extends (...args: any) => any>(f:F, ...args: MParams<F>): MVal<ReturnType<F>> | |
const inc = (a: number):number => a + 1 | |
const sum = (a: number, b:number):number => a + b | |
const x1 = apply(inc, 3) | |
const x2 = apply(sum, 3, 4); | |
const x = mApply(sum, 3, null); | |
const y = mApply(inc, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/v2/en/play?#code/C4TwDgpgBAcgrgGwVAvFOA7AJhAZgSwwiygB8oNEEBYAKFEigFkA1AQwQB4AVAPlSjcysKnQbR4SfLhA9+aKAG86UVVADaABSiEoAawggA9rkEBdAFzN2XblrO8A3HQC+z+uGhNNbAE5sAWwBnHigIAA9gCGwgqAAKADokvwBzIKs2DBAASlR+TJB5QTDI6KxYxOTfNKtCXAhfKE1clHysqAB+ESkZTk1+KyIANwb3OhwAYwQ-aFxMCeB8IwwoNjAwBFkAMRKomPikhNT01ayWtsK43AstgBooQ+OrH38AiCjfEK3ebKsAJXecF8GG4nk43zo4wgUxmUDmGAWSxWAQAgutNuDdmUKo9qicCudTpdrncHlUaswXoEvj8rKwOJwAcAgSCwd9eJDaBNlkFgDoEQI4mxBnAAgAjBq-Sjihp5VZQADUUAAjHRuRheVAgqLBcKKKKJb57mKLNLDVKDbLWvKlWLOerNeFlQI1hsQHFCBN7gBmbJqnl88IAJhd6Pd2oCPvuABZsmMuQGoOEBKiw3EI1H9Ug4-6NXyQCm0W6PQj7pRs+4gA