Open aspirisen opened 1 year ago
That's the major reason I avoid namespaces in TS - the Ruby-esque ability to reopen and edit a definition in my experience makes code much harder to reason about.
If you want to compose modules, imo you should use the current tools and import + re-export, rather than smooshing the code together.
@ljharb merging modules is just one of the possibilities, anyway they will be in one file. Actually, I've never used namespaces merging in TypeScript.
But merging namespace with function is pretty common use case to add logically related things to the function. Probably this will be the target use case for declaration merging. Yes, you will be able to merge modules by that way, but I can hardly imagine why one will need it since everything is in the same file.
That's the major reason I avoid namespaces in TS - the Ruby-esque ability to reopen and edit a definition in my experience makes code much harder to reason about.
If you want to compose modules, imo you should use the current tools and import + re-export, rather than smooshing the code together.
What if we optionally declare if a module can be merged? Something like partial classes in C#. This makes the reader aware that some members of the module might come from other merged declarations.
// ok
partial module A {
export const foo = ...;
}
// ok
partial module A {
export const bar = ...;
}
// ok
module A {
export const foo = ...;
}
// Redeclaration error
module A {
export const bar = ...;
}
This proposal does not permit merging. The code sample at the beginning of the thread would cause an error at parse time. (However, this is not meant to pass judgement on TS declaration merging, which I don’t know if the world could live without!)
Hello, will it be possible to merge declarations, like that:
This is how
namespaces
works in typescript (previously they called modules). PlaygroundOne huge benefit of namespaces is that they can be merge with functions declarations. Playground
This is a widely used pattern i.e. in react. Playground