-
Notifications
You must be signed in to change notification settings - Fork 163
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
A way to merge with existing types #42
Comments
@toddheslin Hi, TypeBox provides are a couple of ways to approach this. The recommended way would be to leverage Type.Union([...])import { Static, Type } from '@sinclair/typebox'
// export type currencies_enum = 'AED' | 'AUD' | 'BRL' | 'CAD' | ...
export type CurrencyKind = Static<typeof CurrencyKind> // hover to inspect type.
export const CurrencyKind = Type.Union([
Type.Literal('AED'),
Type.Literal('AUD'),
Type.Literal('BRL'),
Type.Literal('CAD'),
// ... and so on
])
export type Body = Static<typeof Body> // hover to inspect type.
const Body = Type.Object({
message: Type.String(),
full_name: Type.String(),
preferred_name: Type.String(),
email: Type.String({ format: 'email' }),
phone: Type.String(),
password: Type.String({ minLength: 8 }),
currency: CurrencyKind,
})
// example
const body: Body = {
currency: 'AED',
email: 'dave@domain.com',
full_name: 'dave',
preferred_name: 'dave',
phone: '12345678',
password: 'password',
message: 'hello'
} Type.Enum(...)import { Static, Type } from '@sinclair/typebox'
export enum CurrencyEnum {
AED = 'AED',
AUD = 'AUD',
BRL = 'BRL',
CAD = 'CAD',
// ... and so on
}
export type Body = Static<typeof Body> // hover to inspect type.
const Body = Type.Object({
message: Type.String(),
full_name: Type.String(),
preferred_name: Type.String(),
email: Type.String({ format: 'email' }),
phone: Type.String(),
password: Type.String({ minLength: 8 }),
currency: Type.Enum(CurrencyEnum),
})
// example
const body: Body = {
currency: CurrencyEnum.AED,
email: 'dave@domain.com',
full_name: 'dave',
preferred_name: 'dave',
phone: '12345678',
password: 'password',
message: 'hello'
} Just one quick thing, It's wouldn't be possible for TypeBox to infer a JSON Schema from the existing Hope this helps |
Hey @sinclairzx81 thanks for the speedy and super detailed response. I really appreciate it. My problem is that the current Let's say that I choose to go with the union of string literals, is there a way I can reuse the existing type definition that is generated into |
@toddheslin Hi. Unfortunately, this currently isn't possible. TypeScript doesn't provide any mechanisms to turn a TS type (for example Libraries like TypeBox, io-ts and runtypes work around this by providing type builder API's that let developers write Types in a different way as to be able to embed the type information in the JS while leveraging TypeScript's type mapping facilities to turn that back into a static type, but it doesn't really solve the case where you have an existing type and want runtime type info from it. Here's a few links related to this topic.
As far as leveraging TypeBox for your use case, you would need to replicate the Hope this helps |
Closing this one off. |
Thanks so much for your detailed reply @sinclairzx81, hope this is useful for someone else too. |
Firstly, I'm sorry if this is a newbie question. I'm a relatively new convert to typescript and still figuring my way around.
I'm currently using genql to generate a typescript lib from my GraphQL server. I'd like to import that definition into a typebox object (if this is even possible). Here's an example:
Where
currencies_enum
looks like this:Is there a function I can use to merge that
currencies_enum
type into the typebox representation? My primary reason for this is such that I can spit out a consistent typescript and json schema representation of the same type.Thanks in advance. Any insight here would be a learning experience for me. 🙇🏻♂️
The text was updated successfully, but these errors were encountered: