Files
typebox/docs/types/modules.md

1.3 KiB

Home | Installation | Usage | Types | Values | Syntax | TypeRegistry | TypeCheck

Module Types

Module types are containers for a set of referential types. Modules act as namespaces, enabling types to reference one another via string identifiers. Modules support both singular and mutually recursive references, as well as deferred dereferencing for computed types such as Partial. Types imported from a module are expressed using the Json Schema $defs keyword.

const Module = Type.Module({
  PartialUser: Type.Partial(Type.Ref("User")), // TComputed<'Partial', [TRef<'User'>]>

  User: Type.Object({
    // TObject<{
    id: Type.String(), //   user: TString,
    name: Type.String(), //   name: TString,
    email: Type.String(), //   email: TString
  }), // }>
});
const User = Module.Import("User"); // const User: TImport<{...}, 'User'>

type User = Static<typeof User>; // type User = {
//   id: string,
//   name: string,
//   email: string
// }

const PartialUser = Module.Import("PartialUser"); // const PartialUser: TImport<{...}, 'PartialUser'>

type PartialUser = Static<typeof PartialUser>; // type PartialUser = {
//   id?: string,
//   name?: string,
//   email?: string
// }

Back to Home