๐งwithDefaults
Like Function.prototype.bind() but for a function that receives their parameters wrapped in an object.
Quick example
import { withDefaults } from "tsafe/lab/withDefaults";
function sum(params: { x: number; y: number; z: number }): number {
const { x, y, z } = params;
return x + y + z;
}
// sumWd is of type: (params: { y: number; z: number; })=> number
const sumWd = withDefaults(sum, { x: 10 });
console.log(sumWd({ y: 1, z: 2 })); // Prints "13" ( 10 + 1 + 2 )
console.log(sumWd({ y: 3, z: 4 })); // Prints "17" ( 10 + 3 + 4 )
console.log(
sumWd({
y: 3,
z: 4,
defaultsOverwrite: {
x: [20],
},
})
); // Prints "27" ( 20 + 3 + 4 )In greater detail


Overwriting the injected value:
Last updated
Was this helpful?