ts-library

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TypeScript Library Development

TypeScript类库开发

Patterns for authoring high-quality TypeScript libraries, extracted from studying unocss, shiki, unplugin, vite, vitest, vueuse, zod, trpc, drizzle-orm, and more.
本文中的模式源自对unocss、shiki、unplugin、vite、vitest、vueuse、zod、trpc、drizzle-orm等优秀项目的研究,旨在指导开发高质量的TypeScript类库。

When to Use

适用场景

  • Starting a new TypeScript library (single or monorepo)
  • Setting up package.json exports for dual CJS/ESM
  • Configuring tsconfig for library development
  • Choosing build tools (tsdown, unbuild)
  • Designing type-safe APIs (builder, factory, plugin patterns)
  • Writing advanced TypeScript types
  • Setting up vitest for library testing
  • Configuring release workflow and CI
For Nuxt module development: use
nuxt-modules
skill
  • 启动新的TypeScript类库项目(单仓库或单体仓库)
  • 配置package.json的exports以支持CJS/ESM双格式
  • 为类库开发配置tsconfig
  • 选择构建工具(tsdown、unbuild)
  • 设计类型安全的API(构建器、工厂、插件模式)
  • 编写高级TypeScript类型
  • 配置vitest用于类库测试
  • 配置发布工作流与CI
针对Nuxt模块开发:请使用
nuxt-modules
技能

Quick Reference

快速参考

Working on...Load file
New project setupreferences/project-setup.md
Package exportsreferences/package-exports.md
tsconfig optionsreferences/typescript-config.md
Build configurationreferences/build-tooling.md
ESLint configreferences/eslint-config.md
API design patternsreferences/api-design.md
Type inference tricksreferences/type-patterns.md
Testing setupreferences/testing.md
Release workflowreferences/release.md
CI/CD setupreferences/ci-workflows.md
开发场景加载文件
新项目搭建references/project-setup.md
包导出配置references/package-exports.md
tsconfig选项配置references/typescript-config.md
构建配置references/build-tooling.md
ESLint配置references/eslint-config.md
API设计模式references/api-design.md
类型推断技巧references/type-patterns.md
测试环境搭建references/testing.md
发布工作流配置references/release.md
CI/CD配置references/ci-workflows.md

Loading Files

文件加载建议

Consider loading these reference files based on your task:
  • references/project-setup.md - if starting a new TypeScript library project
  • references/package-exports.md - if configuring package.json exports or dual CJS/ESM
  • references/typescript-config.md - if setting up or modifying tsconfig.json
  • references/build-tooling.md - if configuring tsdown, unbuild, or build scripts
  • references/eslint-config.md - if setting up ESLint for library development
  • references/api-design.md - if designing public APIs, builder patterns, or plugin systems
  • references/type-patterns.md - if working with advanced TypeScript types or type inference
  • references/testing.md - if setting up vitest or writing tests for library code
  • references/release.md - if configuring release workflow or versioning
  • references/ci-workflows.md - if setting up GitHub Actions or CI/CD pipelines
DO NOT load all files at once. Load only what's relevant to your current task.
请根据当前任务加载对应的参考文件:
  • references/project-setup.md - 若正在启动新的TypeScript类库项目
  • references/package-exports.md - 若正在配置package.json的exports或CJS/ESM双格式
  • references/typescript-config.md - 若正在搭建或修改tsconfig.json
  • references/build-tooling.md - 若正在配置tsdown、unbuild或构建脚本
  • references/eslint-config.md - 若正在为类库开发配置ESLint
  • references/api-design.md - 若正在设计公共API、构建器模式或插件系统
  • references/type-patterns.md - 若正在处理高级TypeScript类型或类型推断
  • references/testing.md - 若正在配置vitest或为类库代码编写测试
  • references/release.md - 若正在配置发布工作流或版本管理
  • references/ci-workflows.md - 若正在配置GitHub Actions或CI/CD流水线
请勿一次性加载所有文件。仅加载与当前任务相关的文件。

Key Principles

核心原则

  • ESM-first:
    "type": "module"
    with
    .mjs
    outputs
  • Dual format: always support both CJS and ESM consumers
  • moduleResolution: "Bundler"
    for modern TypeScript
  • tsdown for most builds, unbuild for complex cases
  • Smart defaults: detect environment, don't force config
  • Tree-shakeable: lazy getters, proper
    sideEffects: false
Token efficiency: Main skill ~300 tokens, each reference ~800-1200 tokens
  • ESLint优先:使用
    "type": "module"
    并输出
    .mjs
    格式
  • 双格式支持:始终同时支持CJS和ESM用户
  • 现代TypeScript使用
    moduleResolution: "Bundler"
  • 多数构建场景使用tsdown,复杂场景使用unbuild
  • 智能默认:自动检测环境,不强制配置
  • 支持Tree-shaking:使用延迟获取器,正确设置
    sideEffects: false
令牌效率:主技能约300令牌,每个参考文件约800-1200令牌