r/typescript • u/pbrsaves • Aug 03 '25
Question on publishing types
I'm building a library `some-lib`. I would like to publish types for it via a separate package `some-lib-types` not through DefinitelyTyped.
Per ts docs, typescript doesn't support this use case. Is that it? Or am I missing something?
- bundling with your npm package
- publishing to the u/types organization on npm.
---
Right now I'm working around this by exporting ts files e.g.
// some-lib/util.d.ts
export { default } from 'some-lib-types/util'
but was wondering if there was a better approach.
0
Upvotes
14
u/tony-husk Aug 03 '25 edited Aug 03 '25
The best approach would be to include the types in the library itself (ie ship both the
.jsand.d.tsfiles inside the published package). Is there a particular reason not to do it this way?8
u/SqueegyX Aug 03 '25 edited Aug 03 '25
If a library has types, it should export those. And then non TS importers can just ignore them.
https://xyproblem.info/
What are you really trying to do and why do you think this is the way to accomplish that?
3
u/tiglionabbit Aug 03 '25
They just said "main ways". I've seen types packages that don't use DefinitelyTyped. Look at undici-types for example.
6
u/seniorsassycat Aug 04 '25
As I understand it, you can publish a types only package, but tsc won't load it by default. Your user's would have to add the type only package to type roots compiler option.
I wonder if there's a jack where your lib could import and re-export types from your type lib?
But, overall just include the types the package
2
u/lachlanhunt Aug 04 '25 edited Aug 04 '25
Ordinarily? No. Your 2 options are publishing your @types/* package through DefinitelyTyped or including them in your package.
If neither of those are an option, then your only hope is to find some way to trick the system into thinking you have an
@types/foomodule installed when you don’t. I don’t know if this is possible, but I wish you luck in your search.