Looking for the most elegant Typescript solution for a function with optional parameters.
The function doSomething(num, config?)
takes a number and an optional config object with a few optional parameters.
If the config type is defined as { acceptNegative?: boolean, isBig?: boolean }, then these should all be valid calls:
doSomething(num);
doSomething(num, { acceptNegative: true });
doSomething(num, { acceptNegative: true, isBig: false });
In addition, default values should be set for cases where:
- User doesn't provide config object =>
doSomething(num);
- User provides incomplete data in config object =>
doSomething(num, { acceptNegative: true });
Seeking the most elegant implementation for this scenario. Any suggestions?