@smartregionlab/cop-types
v1.0.6
Published
Shared TypeScript types for frontend and backend
Readme
cop-types
This repository acts as an unpublished npm package that can be consumed by other applications.
It contains a collection of types that are shared among the cop applications, using the cop-backend application as a baseline.
Any types that are not needed in the backend application will not be published to this repository, and need to be implemented locally.
Publishing/Changing types
- Clone the git repository
git clone [email protected]:hslu/research/infovis/smartregion/pilots/cop/cop-types.git- Move into the newly downloaded repository
cd cop-types- Add/update/remove the type from the
src/*-types.tsfile
const SamplePodcastSchema = z.object({
id: z.string(),
title: z.string(),
description: z.string(),
authorId: z.string(),
createdAt: z.date(),
});
export type SampleZodPodcast = z.infer<typeof SamplePodcastSchema>;(Ensure to update any corresponding exports)
If a new file was created, update the exports in
src/index.tsPush the new version to the main branch
git add .
git commit -m "chore: added podcast type"- Bump the npm package version ("version": "X.X.X"), using the npm utility:
- For minor changes/bug fixes:
npm version patch(change of 0.0.1) - For bigger changes/new types:
npm version minor(change of 0.1.0) - For breaking changes:
npm version major(change of 1.0.0)
The
npm versionutility updatespackage.json,package-lock.jsonand creates a new git tag with the updated version. It also creates a new git commit locally.
- Publish the updated version
npm publish- Push the updated files to remote
git push origin main
git push --tagsConsuming types
- Install the type package from source
npm install @smartregionlab/cop-types- Update an already installed version of the package
# Update all packages
npm update
# Pull newest version of only the cop-types package
npm install @smartregionlab/cop-types@latest
# Install specific version i.e. 1.2.3
npm install @smartregionlab/[email protected]- Import and use the type in the current .ts file
import { SampleZodPodcast } from "@srl/cop-types"
const podcast: SampleZodPodcast = {
id: "123",
title: "My Podcast",
description: "A show about coding",
authorId: "author-1",
createdAt: new Date(),
};In this case, Zod will now ensure runtime typesafety through the external cop-types package. If we define something that is not allowed, ie:
const podcast: SampleZodPodcast = {
id: "123",
title: "My Podcast",
description: "A show about coding",
authorId: "author-1",
createdAt: "a string instead of a date",
};Zod will throw an error:
error TS2322: Type 'string' is not assignable to type 'Date'.