A
arktype•5d ago
dibbo

Best way of sharing/compiling types?

I have two applications, in one I have defined types like
const loginPayload = type({
email: email,
password: "string",
"+": "delete",
});
const loginPayload = type({
email: email,
password: "string",
"+": "delete",
});
I can make use of this type throughout the application by inferring i.e.
type LoginPayload = typeof loginPayload.infer;
type LoginPayload = typeof loginPayload.infer;
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
const loginPayload = type({
email: email,
password: "string",
"+": "delete",
});

const inferred = loginPayload.infer;

export type LoginPayload = typeof inferred;
const loginPayload = type({
email: email,
password: "string",
"+": "delete",
});

const inferred = loginPayload.infer;

export type LoginPayload = typeof inferred;
Is this the recommended approach for this use case or has something better been introduced since? Thanks!
3 Replies
ssalbdivad
ssalbdivad•5d ago
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 haha
dibbo
dibboOP•5d ago
Close 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
ssalbdivad
ssalbdivad•5d ago
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-monorepo
Colin McDonnell @colinhacks
Live types in a TypeScript monorepo
Want results from more Discord servers?
Add your server