-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Embedding types in module/namespace declarations #35030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
No because I will often collect multiple files and export them under a single package namespace import * as a from './a'
import * as b from './b'
import * as c from './c'
export const myLibrary = {
...a,
...b,
...c,
}
export default myLibrary
export declare namespace myLibrary {
export type A<T = any> = a.A<T>
export type B = b.B
export type C = c.C
} making the contents accessible against the namespace import myLibrary from 'my-library'
function foo(bar: myLibrary.A) {
console.log(bar)
}
const a = new myLibrary.A()
foo(a) This can get annoying and error prone if you have many types export declare namespace myLibrary {
export type A<T = any> = a.A<T>
export type B = b.B
export type B2 = b.B2
export type C = c.C
export type C2<T1 = string, T2 = number, T3 = any> = c.C2<T1, T2, T3>
// etc
} |
At least you can avoid redeclaring type parameters: declare namespace myLibrary {
export import A = a.A;
// ...
} If you didn’t need it to be a default export, it would just be export * from './a';
export * from './b';
export * from './c'; and import * as myLibrary from 'my-library' And probably a less error-prone workaround that maintains the default export is to consolidate in another file: // barrel.ts
export * from './a'
export * from './b'
export * from './c' // index.ts
import * as myLibrary from './barrel'
export default myLibrary But yeah, I guess there’s no 100% kludge-free way to do what you’re trying to do. But even if there was, I would personally not be a fan of a module structure like this as an API consumer. By barreling everything up and reexporting as a default export, you prevent consumers from doing named imports of individual types: |
Hey nice! That's a clever work around! Just to extend it to enable named exports: // index.ts
import * as myLib from './barrel'
export {myLib}
export default myLib This can be used both like import myLib from 'my-lib' and import { myLib } from 'my-lib' |
My point about named imports isn’t about importing |
I used to feel the same way about imports - always prefer picking out named exports from a library. I started working with Go (Golang) a few years back. Go is syntactically similar to TypeScript and something that really stuck out was the ergonomics of grouping logic into "packages". It sounds stupid but professionally, it has resulted in significant improvements of code quality. It's unconventional for JS/TS, but I am happy that the language is flexible enough to allow for this coding style. |
Search Terms
module namespace declaration rexporting
Suggestion
When trying to export types in an
index.ts
, currently one must redefine those types in the barrel.e.g.
Consumer
MyModule
Currently developers must export their variables and export their types too, but types must be redeclared.
Suggestion
Would be nice if developers could just embed the type in the namespace
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: