Last active
April 17, 2020 12:59
-
-
Save GheorgheP/32b6ef1d0b7a1ea6318102494d5ad1c3 to your computer and use it in GitHub Desktop.
Transform a strict function in an function with nullified parameters.
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 Null = undefined | null | |
type MVal<T> = T | Null | |
declare function mFn<T, R>(f:(a:T) => R): { | |
(a: T): R, | |
(a: Null): Null | |
} | |
declare function mFn<T, T2, R>(f:(a:T, b:T2) => R): { | |
(a: T, b:T2): R, | |
(a: Null, b: unknown): Null, | |
(a: unknown, b: Null): Null, | |
} | |
const sum = (a: number, b:number):number => a + b | |
const mSum = mFn(sum); | |
const x = mSum(3, 3) // x: number | |
const y = mSum(3, null) // y: Null | |
const z = mSum(null, 3) // z: 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/C4TwDgpgBAcgrgGwVAvFOA7AJhAZgSwwiygB8oNEEBYAKFEigFkA1AQwQB4AVAPlSjcysKnTo4AxgjYAnaLkwTg+APYYoAWwBiGHgBooAJV4AKXAC4Tbc9wCUqfodvmoAbzpQoVl3ZeG9Hl7WIkjOITS0AL5itJLSclAKGEqq6tq63AbcAEwGxmaW1plQAEY22fYojmHutJ7eggZlOWH+gQ3wSE0umADWGCoA7hhhnQgBdUE9GP1DGN3ho1QT0bQxEmoAzsBQm3AaAg2UGiUQMt3HpzLOl2cOUGxQANSldBsY25oAyvsC6SZ7DS2ADc6y2OwAHn8fhoTABmAxw+wAemRUAhLluMje4KgIGh+3hBkooSgqLxLjGOI+OwAXgTYSTxlAkWS0bTKaJaEA