This demonstration is kept simplistic and straightforward, yet the ultimate objective is far more intricate. It is crucial to grasp the method behind achieving this.
Let's assume there exists a class
class Foo {
bar: string;
baz: number;
bob: any; // could be a function
bad() {
return true;
};
}
and also a function
const init = (options: InitOptions, bar?: string, baz?: number, bob?: any) => {
const foo = new Foo();
foo.bar = bar ?? options.fallbacks.bar;
foo.baz = baz ?? options.fallbacks.baz;
foo.bob = bob ?? options.fallbacks.bob;
}
The question arises - how can one structure the type InitOptions to replicate the following pattern
type InitOptions = {
fallbacks = {
bar: string;
baz: number;
}
}
while avoiding redundancy? For instance, fallbacks
should form an object where each key corresponds to a property within Foo
.