I have a typescript function with some params like that
myFunction(allData: Array<any>, selectedItems: Array<any>, onlyValues: boolean = false, values: Array<any>) {
console.log(allData);
console.log(selectedData);
console.log(onlyValues);
console.log(values);
}
If onlyValues is set to true, I want values to be required, else, I don’t need values
How to do this please
>Solution :
You could try with a guarded type:
type Values = {onlyValues: true, values: Array<any>} | {onlyValues: false}
Then you would pass (allData: Array<any>, selectedItems: Array<any>, v: Values) to the function.
You can try this TS Playground.