typebox icon indicating copy to clipboard operation
typebox copied to clipboard

Make Nullable a standard type.

Open InputNeuron opened this issue 1 year ago • 2 comments

I know it's easy to make my own Nullable type (especially since it's in the front page on the docs) but it would rock to have one in Types directly.

I don't mind sending in a PR if you're game.

BTW. Now that OpenAI supports json schema I think this is a super valuable project. Zod is nice but it doesn't have native JSON schema support so this gives Typebox a huge win.

InputNeuron avatar Dec 08 '24 05:12 InputNeuron

@burtonator Hi,

Yeah, this one gets asked about quite a lot. TypeBox may get a NonNullable<T> type but Nullable<T> is still considered out of scope. There are two reasons why. The first is that TypeScript doesn't provide a Nullable<T> as a utility type, the second is that there is still some ambiguity and confusion around how to represent Nullable<T> depending on the environment.

// Expression of Nullable in Json Schema
//
const T = Type.Nullable(Type.String()) // const T = { anyOf: [{type: 'string'}, {type: 'null'}] }

// OpenAPI Extended Representation for Nullable
//
const S = Type.Nullable(Type.String()) // const S = { type: 'string', nullable: true }

TypeScript does however provide a NonNullable<T> type so would be a candidate for inclusion.

https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype

For now, TypeBox just follows TS and requires the construction of Nullable<T> via generics.

// TS

type Nullable<T> = T | null

// TB

const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()])

By keeping it open and generic, it just means TB can cater to multiple representations for nullable (as per legacy OpenAPI)

const Nullable = <T extends TSchema>(type: T) => Type.Unsafe<Static<T> | null>({ ...type, nullable: true })

BTW. Now that OpenAI supports json schema I think this is a super valuable project. Zod is nice but it doesn't have native JSON schema support so this gives Typebox a huge win.

Ah interesting. I'm actually yet to look into this in any depth. I note that OpenAI provides some adapter for Zod in their SDK, but I don't know the domains in which it's being utilized for anything (other than generative data conforming to some schema specification).

Do you have some insights into some of the use cases where OpenAI (or other LLM's) are using schematics for generative things? I did have a few ideas for some side projects utilizing LLM's for code generation, but curious how the ecosystem is beginning to leverage LLM's (and more so if TypeBox should be doing more to help accommodate generative workflows)

Interested to hear your thoughts. Cheers S

sinclairzx81 avatar Dec 08 '24 07:12 sinclairzx81

@burtonator Hiya,

Might close off this issue for now as Nullable<T> is considered to be out of scope. Still keen to hear some of the high level use cases where Json Schema / Zod are being used in tandem with LLM's though. If you get some time, would welcome any insights into the ways these models are being utilized with schematics (again, not sure if TB could be offering more in this regard)

All the best S

sinclairzx81 avatar Dec 09 '24 03:12 sinclairzx81