#Declaration merging across multiple files?

1 messages · Page 1 of 1 (latest)

ocean karma
#

I have the following directory structure,

some-process/
    index.ts

where index.ts looks something like this:

class SomeProcess {}

namespace SomeProcess {
    export class Output {}
}

export default SomeProcess;

I would like to factor out Output into its own file with index.ts; output.ts. However, I can't figure out how to do that while maintaining the import/access signature (import SomeProcess from "@app/some-process" and SomeProcess.Output, respectively)

is this possible?

south hound
#

There's a lot of footguns in setting this up. Not only are there ways you can screw it up in TS but you can also screw up other tools.

For a random example of a breakage if you do this a particular way: https://github.com/un-ts/eslint-plugin-import-x/issues/308. Another issue would be another package renaming your library.

#

So for that reason my recommendation is to add imports to your package.json to be name-agnostic.

{
    ...
    "imports": {
        "#index": {
            "import": "./index.ts"
        }
    }
}

And then you can write:

export {}; // An import/export is necessary to make sure this file counts as a module and doesn't override `#index`.

declare module "#index" {
    namespace SomeProcess {
        export class Output {}
    }
}
#

This approach side steps every issue I know of

#

The reason for imports is to avoid writing:

export {};

declare module "your-package-name/index" {
    namespace SomeProcess {
        export class Output {}
    }
}

As that'd lock in a specific name for your package and prevent people from doing npm add -D alias@your-package-name