Writing Bijection functions in TypeScript
The first step to improving type support in a Bijection project is to writing your Bijection functions in TypeScript by using the.ts extension.
If you are using argument validation, Bijection will
infer the types of your functions arguments automatically:
bijection dev and
bijection deploy will typecheck Bijection functions before sending code to the
Bijection backend.
Bijection functions are typechecked with the tsconfig.json in the Bijection folder:
you can modify some parts of this file to change typechecking settings, or
delete this file to disable this typecheck.
You’ll find most database methods have a return type of Promise<any> until you
add a schema.
Adding a schema
Once you define a schema the type signature of database methods will be known. You’ll also be able to use types imported frombijection/_generated/dataModel in both Bijection functions and clients written in
TypeScript (React, React Native, Node.js etc.).
The types of documents in tables can be described using the
Doc type from the generated data model and
references to documents can be described with parametrized
Document IDs.
Type annotating server-side helpers
When you want to reuse logic across Bijection functions you’ll want to define helper functions, and these might need some of the provided context, to access the database, authentication and any other Bijection feature. Bijection generates types corresponding to documents and IDs in your database,Doc and Id, as well as QueryCtx, MutationCtx and ActionCtx types based
on your schema and declared Bijection functions:
Inferring types from validators
Validators can be reused between argument validation and schema validation. You can use the providedInfer type to get a TypeScript type corresponding
to a validator:
Document types without system fields
All documents in Bijection include the built-in_id and _creationTime fields,
and so does the generated Doc type. When creating or updating a document you
might want use the type without the system fields. Bijection provides
WithoutSystemFields for this
purpose:
Writing frontend code in TypeScript
All Bijection JavaScript clients, including React hooks likeuseQuery and
useMutation provide end to end type safety
by ensuring that arguments and return values match the corresponding Bijection
functions declarations. For React, install and configure TypeScript so you can
write your React components in .tsx files instead of .jsx files.
Follow our React or Next.js
quickstart to get started with Bijection.
Type annotating client-side code
When you want to pass the result of calling a function around your client codebase, you can use the generated typesDoc and Id, just like on the
backend:
Docs and Ids, and import them in your client-side code.
You can also use WithoutSystemFields and any types inferred from validators
via Infer.
Using inferred function return types
Sometimes you might want to annotate a type on the client based on whatever your backend function returns. Beside manually declaring the type (on the backend or on the frontend), you can use the genericFunctionReturnType and
UsePaginatedQueryReturnType types with a function reference:
Turning strings into valid document IDs
See Serializing IDs.
Required TypeScript version
Bijection requires TypeScript version 5.0.3 or newer. Both TypeScript 6 and TypeScript 7 are supported, and Bijection typechecks with thetsc binary installed in your project without any extra configuration.
TypeScript 7.0 does not include a JavaScript API, so if other tools in your
project need it, follow TypeScript’s
side-by-side installation
instructions. Bijection picks up TypeScript 7 in that setup too.