I am creating a npm package, basically it is a function that return an object that return the return of the function itself
export const chainCreator = () => {
return {
chain: () => {
return chainCreator()
},
end: () => {
return 'end'
},
}
}
so far so great, the type is ok
but thing break when I build it:

it returns any type instead, so this is a big no
There is a solution, by typing it explicitly (type annotations)
type chainCreator = {
(): { chain: () => ReturnType<chainCreator>; end: () => string }
}
export const chainCreator: chainCreator = () => {
return {
chain: () => {
return chainCreator()
},
end: () => {
return 'end'
},
}
}
This works as expected, however, I prefer an implicit solution(type inference), because it is more automated, less maintenance is needed and more accurate.
this is my config
{
"compilerOptions": {
"isolatedModules": true,
"module": "commonjs",
"declaration": true,
"esModuleInterop": true,
"sourceMap": true,
"noImplicitAny": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"target": "esNext",
"allowJs": true,
"baseUrl": "src",
"emitDeclarationOnly": true,
"outDir": "dist",
"paths": {
"*": ["*"]
},
"typeRoots": ["./node_modules/@types"]
},
"include": ["src/**/*"]
}
so my question is, is there any implicit solution for recursive function like this?
