Skip to content

api‐zimic

github-actions[bot] edited this page Aug 25, 2024 · 2 revisions

API reference: zimic

Contents


The module zimic exports general resources and utility types used by Zimic.

Tip

All APIs are documented using JSDoc and visible directly in your IDE.

zimic utility types

JSONValue

Represents or validates a type that is compatible with JSON.

import { type JSONValue } from 'zimic';

// Can be used as a standalone type:
const value: JSONValue = {
  name: 'example',
  tags: ['one', 'two'],
};
import { type JSONValue } from 'zimic';

// Can be used with a type argument to validate a JSON value:
type ValidJSON = JSONValue<{
  id: string;
  email: string;
  createdAt: string;
}>;

// This results in a type error:
type InvalidJSON = JSONValue<{
  id: string;
  email: string;
  createdAt: Date; // `Date` is not a valid JSON value.
  save(): Promise<void>; // Functions are not valid JSON values.
}>;

Important

The input of JSONValue and all of its internal types must be declared inline or as a type aliases (type). They cannot be interfaces.

JSONSerialized

Recursively converts a type to its JSON-serialized version. Dates are converted to strings and keys with non-JSON values are excluded.

import { type JSONSerialized } from 'zimic';

type SerializedUser = JSONSerialized<{
  id: string;
  email: string;
  createdAt: Date;
  save(): Promise<void>;
}>;
// {
//   id: string;
//   email: string;
//   createdAt: string;
// }