Best way of sharing/compiling types?
I have two applications, in one I have defined types like
I can make use of this type throughout the application by inferring i.e.
What would be the best way of sharing this type with the other (separate) application? I've previously been able to use
tsc
to correctly compile the types into a single file, but with recent versions of ArkType/TS (I'm not sure which updated to impact this), the types compile to any
.
I came across the following approach in another thread which results in correctly compiled types
Is this the recommended approach for this use case or has something better been introduced since?
Thanks!3 Replies
If it's in a monorepo and you have the ability to have the types resolve to
.ts
, would definitely recommend that.
Otherwise, unfortunately we don't really have much control over how tsc serializes this stuff. IMO, any cases where types are correctly inferred in source but not in .d.ts
output is a TypeScript bug.
That approach is probably your best bet if you need to resolve from .d.ts
. @Andarist actually initially found it while we were working on Zod resolution in .d.ts
, so while some details might vary, unfortunately type inference degradation in .d.ts
is pretty parse for the course for schema libraries.
At some point maybe we can support some kind of custom .d.ts
output, but it's not on the immediate horizon
"parse for the course" :KEKW:
Did not mean to type that hahaClose enough! 😄
Thats cool though, thanks for the reply! A monorepo might make more sense for this setup.. I'll draw up some pros and cons and see where I land haha
If you do end up going in that direction would recommend this setup for resolving types/src to
.ts
during dev:
https://colinhacks.com/essays/live-types-typescript-monorepoColin McDonnell @colinhacks
Live types in a TypeScript monorepo