Sometimes a method has a required set of option parameters. e.g. min and max.
function valueBetween(range: {min: number, max: number} = { min: 0, max: 10 }) {} βοΈ
In this case it should be OK to use object default parameters because both min and max are required.
Or to be more precise, it should be ok if defined default properties === required properties.
It is not OK if any optional property is provided.
Yes, it it possible for plain JS users to fall through, but they are also likely to fail with the types in general or cause conflicts due to mismatching boundaries:
valueBetween({min: 'a', max: 10}); β
valueBetween({min: 100}); β
Otherwise it is impossible to implement the method parameter default in a type safe way.
function valueBetween(range: {min: number, max: number} = {} β) { // ts-error
const { min = 0, max = 10 } = options;
}
function valueBetween(range: {min: number, max: number} = { min: 0, max: 10 }) {} :check βοΈ
function valueBetween(range: {min: number, max: number, step: number} = { min: 0, max: 10 }) {} β
function valueBetween(range: {min: number, max: number, step?: number} = { min: 0, max: 10, step: 1 }) {} β
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too