I want to build an API, inspired by jest it
function, which will look like this:
Typescript
interface Something {
(param: number): void
modifier: Something
anotherModifier: Something
}
So I'll be able to use it like this:
s()
s.modifier()
s.anotherModifier.modifier()
If I was building it in pure JS, I'd make a few different functions, and then give them properties that would point towards one another:
const pure = (a) => {}
const modifier = (a) => {}
const anotherModifier = (a) => {}
for(const handler of [pure, modifier, anotherModifier]) {
handler.modifier = modifier
handler.anotherModifier = anotherModifier
}
But in TS I can't do this, as an object has to have all required properties when it's created, so I'm forced to use casts, but: (1) it is ugly, and (2) I feel like there's a much easier pattern to construct this.
So, is there a standard and type safe pattern (or library) which is commonly used to construct objects like these in Typescript? Maybe there are existing open source projects written in Typescript which can serve as example for such task?
This question is building on question about function properties — it's not duplicate, I need to do more things which are more specific.