Typescript is awesome, types are awesome, but it's not so easy for some scenarios.
I'm fighting with typescript for a few days, i can't understand where to place my types that need to be used across whole project.
I know that that types can be included in *.ts file, and they will be erased when compiled. But, if i need that type in another module than i need to import it with relative path. But it seems ugly.. if i move that file to another location than i will break half of project.
So i went with idea that types should be independent of the place where it's used.
I've read about declaration merging, and it looks like that is what i want.
My objectives:
I want to build a library (for nodejs) that will be used in another typescript project and (maybe) pure javascript project.
Obviously, the output of my library will be *.js and *.d.ts files. I don't want publish *.ts files because i want compile them once and it may be used in pure js project (i don't want bring typescript compiler there)
One file (module), contains single function.
That function has input and output. Types for input and output is written in d.ts file
declare namespace library.foo {
type main = () => out
type out = {
name: string
}
}
and next to declaration i place file with implementation (foo.ts):
createFoo: library.foo.main = () {
return { name: "baz"}
}
After running tsc i i see this output for foo.d.ts in dist folder:
declare const createFoo: library.foo.main
my hand written declaration file has left in source folder, and it's not in dist folder.
Is there any option in tsc that will emit single declaration file with all my types? That file will be published with generated .js files and all .t.ds files