Project: TypeScript Utility Library
Introduction
This project packages a tiny typed utility library for npm: generic helpers, emitted .d.ts declarations, and a README with usage examples. You practice library tsconfig, Rollup or tsc emit, and versioning—skills for shared internal packages and open source.
Prerequisites
Project Goals
- Export
groupBy,uniqueBy, andassertNever - Emit ESM + CJS (or ESM-only) with
.d.ts - Document generic usage in README
Setup
bash
mkdir ts-mini-utils
cd ts-mini-utils
npm init -y
npm install --save-dev typescript rollup @rollup/plugin-typescripttext
ts-mini-utils/
├── package.json
├── tsconfig.json
├── rollup.config.mjs
├── src/
│ └── index.ts
└── README.mdtsconfig.json:
json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"rootDir": "src",
"strict": true
},
"include": ["src"]
}Library Code
src/index.ts:
typescript
Code explanation:
groupBypreserves key typeKin the return recordassertNeversupports exhaustiveswitchin consumer apps
Rollup Config
rollup.config.mjs:
javascript
import typescript from "@rollup/plugin-typescript";
export default {
input: "src/index.ts",
output: [
{ file: "dist/index.js", format: "esm", sourcemap: true },
{ file: "dist/index.cjs", format: "cjs", sourcemap: true },
],
plugins: [typescript({ tsconfig: "./tsconfig.json" })],
};package.json Exports
json
README Usage Section
Document in README.md that consumers import named exports:
typescript
import { groupBy, uniqueBy } from "ts-mini-utils";
const users = [
{ id: "1", role: "admin", name: "Ada" },
{ id: "2", role: "member", name: "Lin" },
];
const byRole = groupBy(users, (u) => u.role);
const uniqueNames = uniqueBy(users, (u) => u.name);Build and Local Test
bash
npm run build
npm packIn another project:
bash
npm install ../ts-mini-utils/ts-mini-utils-0.1.0.tgzSemantic Versioning
- Patch — bug fixes, no API change
- Minor — backward-compatible features
- Major — breaking type or runtime changes
Document breaking changes in CHANGELOG.
Extensions
- Add Vitest unit tests
- Publish to npm with
npm publish --access public - Add
exportssubpaths for tree-shaking modules
FAQ
tsc only without Rollup?
Run tsc with declaration: true—fine for simple libraries; Rollup bundles multiple formats.
Type tests?
tsd or expect-type verify inferred types in tests.
Peer dependency on TypeScript?
Libraries usually do not require TS at runtime—types ship in .d.ts only.