Add Nullable type #820
fernandortec
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey, I was working on this project and I was really tired of doing
Type.Optional(Type.Union([Type.String(), Type.Null()]))
, just to make a nullable type, so I came up with a nullable type-safe function.Does it make sense to implement this as a new type?
`
function Nullable(
property: T,
options?: TypeBox.ObjectOptions,
): TypeBox.TOptional<TypeBox.TUnion<[T, TypeBox.TNull]>> {
return TypeBox.Optional(
TypeBox.Union([property, TypeBox.Null()], { ...options }),
);
}
interface TWithNullable extends Omit<typeof TypeBox, ""> {
Nullable: typeof Nullable;
}
export const t: TWithNullable = {
...TypeBox,
Nullable,
};
`
Beta Was this translation helpful? Give feedback.
All reactions