I have a method in typescript as below using named parameters
public foo({x, y, z , m , n} : {x:string, y: number, z: number, m?:string, n?:number}) {
}
parameters m
and n
will be provided from another object like
const default = { m : 'M', n :10, o:6 }
Now I want to call foo like below and I want default parameters will be added without explicitly passing them in the call
foo({x:'x', y: 5, z: 0})
So my question is how to apply default
in the body of foo
or somehow intercepting foo
before call and apply default
public foo({x, y, z , m , n} = {x:string, y: number, z: number, m?:string, n?:number}) {
// for example how to apply default here
}
Please note for simplicity I reduced number of parameters
Also I know these below solutions already I am looking for something with less boilerplate code
public foo({x, y, z , m , n} = {x:string, y: number, z: number, m?:string, n?:number}) {
if (!m) {
m = default.m;
}
if (!n) {
n = default.n;
}
}
or
foo({...default, x:'x', y: 5, z: 0 });