I am working on a module where I have to do something like this:
const foo = () => {
const bar = {
a: () => bar,
b: () => bar,
c: () => bar
}
return bar
}
foo().a().b().c()
Even though all types are implicit typescript is awesome and helps me autocomplete.
Since I have to publish to npm, I let tsc generate the types declarations, however this is what I get in the index.d.ts file:
declare const foo: () => {
a: () => any;
b: () => any;
c: () => any;
};
Which means that whoever install my module will not be able to take full advantage of typescript goodness.
Is there an easy way to solve this problem?
You just need to give TS compiler a small tip to help figure out the types:
Second option - recursion approach
TS compiler puts
any
here:because it is a recursion type. What would you expect to see here?
TS should stop somewhere)