Skip to content

Instantly share code, notes, and snippets.

@GheorgheP
Last active April 17, 2020 12:59
Show Gist options
  • Save GheorgheP/32b6ef1d0b7a1ea6318102494d5ad1c3 to your computer and use it in GitHub Desktop.
Save GheorgheP/32b6ef1d0b7a1ea6318102494d5ad1c3 to your computer and use it in GitHub Desktop.
Transform a strict function in an function with nullified parameters.
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