Skip to main content

TypeScript Configuration

Relevant source files This document provides a detailed explanation of the TypeScript configuration in @ui8kit/core, covering compilation settings, type generation, path aliases, and module resolution. The configuration is defined in tsconfig.json1-26 and integrates with the build system documented in Build System. For information about the distribution artifacts generated by TypeScript compilation, see Package Structure. For details on how types are used in component APIs, see API Reference.

Configuration Overview

The TypeScript configuration serves three primary functions: compiling source code from src/ to ES2022 modules in dist/, generating type declaration files (.d.ts) for IDE autocomplete and type checking, and establishing path aliases for clean imports. Diagram: TypeScript Compilation Pipeline
Sources: tsconfig.json1-26 package.json22-25

Compilation Target Settings

The configuration targets modern JavaScript environments with ES2022 output, avoiding transpilation of recent language features while maintaining broad compatibility. Compilation Settings Table

Target: ES2022

The target: "ES2022" setting tsconfig.json5 compiles TypeScript to ECMAScript 2022, which includes:
  • Top-level await
  • Class fields and private methods
  • Static initialization blocks
  • Error cause property
  • at() method for arrays
This assumes consumers use modern browsers or Node.js 16+ environments.

Module System: ES2022

The module: "ES2022" setting tsconfig.json6 generates ES modules with .js extensions, enabling tree-shaking in consumer applications. This aligns with the "type": "module" declaration in package.json8

Module Resolution: Bundler

The moduleResolution: "Bundler" setting tsconfig.json7 uses bundler-specific resolution that supports:
  • Extensionless imports (import { Button } from "./Button")
  • Package exports field resolution
  • Conditional exports for different environments
  • Subpath patterns
This works seamlessly with modern bundlers like Vite, Webpack 5, and esbuild. Sources: tsconfig.json5-7 tsconfig.json14-15 package.json8

Type System Configuration

The configuration enables all strict type checking options and generates declaration files for IDE integration. Type System Settings Table

Strict Mode

The strict: true flag tsconfig.json8 enables all strict type checking options:
  • strictNullChecks: Null and undefined are not assignable to other types
  • strictFunctionTypes: Function parameters are checked contravariantly
  • strictBindCallApply: Type-check bind, call, and apply methods
  • strictPropertyInitialization: Class properties must be initialized
  • noImplicitAny: Variables must have explicit types
  • noImplicitThis: this expressions require explicit type
  • alwaysStrict: Parse in strict mode and emit "use strict"
This prevents common errors and ensures type safety across the component library.

Declaration Generation

The declaration: true setting tsconfig.json17 generates .d.ts files alongside compiled JavaScript. These type definitions:
  • Enable IDE autocomplete for all exported components
  • Provide inline documentation from JSDoc comments
  • Support type checking in consumer TypeScript projects
  • Power the "types": "./dist/index.d.ts" entry in package.json32
Diagram: Type Definition Generation Flow

Composite Projects

The composite: true flag tsconfig.json16 enables TypeScript project references, which:
  • Allow faster incremental builds
  • Enable dependency graphs between TypeScript projects
  • Support monorepo architectures documented in SUBMODULE_GUIDE.md
Sources: tsconfig.json8-10 tsconfig.json16-18 package.json32

Path Alias Configuration

Path aliases enable clean imports using @/ prefix and @ui8kit/core package reference. Path Alias Settings

Base URL

The baseUrl: "." setting tsconfig.json20 establishes the project root as the base for all non-relative imports. This enables path resolution relative to the repository root.

Path Mappings

The paths object tsconfig.json21-24 defines two alias patterns: Alias Table

Internal Imports: @/*

The @/* alias maps to ./src/* tsconfig.json22 enabling imports like:
  • import { Block } from "@/core/ui/Block"
  • import { spacingVariants } from "@/core/variants/spacing"
  • import { ThemeProvider } from "@/themes/providers/ThemeProvider"
This eliminates brittle relative paths (../../../core/ui/Block) and makes refactoring safer.

Package Entry Point: @ui8kit/core

The @ui8kit/core alias maps to ./src/index tsconfig.json23 allowing internal files to import from the package as consumers would. This is useful in:
  • Example code and documentation
  • Testing component composition
  • Verifying the public API surface
Diagram: Path Resolution Flow
Sources: tsconfig.json20-24

Build Output Configuration

The configuration controls where compiled files and type definitions are written. Output Settings Table

Root Directory

The rootDir: "src" setting tsconfig.json13 establishes src/ as the root for compilation. This ensures the output directory structure mirrors the source:

Output Directory

The outDir: "./dist" setting tsconfig.json19 writes compiled JavaScript to dist/. This directory is:
  • Included in NPM package via "files": ["dist/**/*"] in package.json39-43
  • Referenced by "main": "./dist/index.js" in package.json31
  • Excluded from version control via .gitignore
  • Generated by the npm run build script in package.json22

Declaration Directory

The declarationDir: "./dist" setting tsconfig.json18 writes .d.ts files alongside .js files in the same directory structure. This colocation:
  • Simplifies package distribution (single dist/ folder)
  • Matches consumer expectations for type locations
  • Enables the "types": "./dist/index.d.ts" entry in package.json32
Sources: tsconfig.json13 tsconfig.json18-19 package.json31-32 package.json39-43

Include and Exclude Patterns

The configuration specifies which files to compile and which to ignore. File Pattern Settings

Include Pattern

The include: ["./src"] setting tsconfig.json2 compiles all TypeScript files within the src/ directory:
  • src/**/*.ts - All TypeScript files
  • src/**/*.tsx - All React component files
  • src/**/*.json - JSON modules (via resolveJsonModule: true)
This captures:
  • Core primitives in src/core/ui/
  • Variant definitions in src/core/variants/
  • UI components in src/components/ui/
  • Layout templates in src/layouts/
  • Theme system in src/themes/
  • Registry metadata in src/registry.json

Exclude Pattern

The exclude: ["./lib", "./dist"] setting tsconfig.json3 prevents compilation of:
  • ./lib/ - Generated artifacts like core-classes.json documented in Build System
  • ./dist/ - Previously compiled output to avoid recursive compilation
Sources: tsconfig.json2-3

Special Compiler Options

Several additional options configure TypeScript’s behavior for specific use cases. Special Options Table

Bun Runtime Types

The types: ["bun-types"] setting tsconfig.json11 includes type definitions for the Bun runtime. This enables:
  • Type checking for Bun-specific APIs
  • Support for bun commands in scripts like bunx buildy-ui@latest scan in package.json26
  • Development with Bun’s fast TypeScript transpiler
The bun-types dependency is declared in package.json62

JSON Module Resolution

The resolveJsonModule: true setting tsconfig.json12 allows importing JSON files as typed modules:
This is used to import src/registry.json throughout the codebase, as documented in Component Registry.

CommonJS Interoperability

The esModuleInterop: true setting tsconfig.json9 enables seamless imports of CommonJS modules in ES module syntax:
This is essential for compatibility with dependencies like class-variance-authority and clsx declared in package.json48-53

Library Type Checking

The skipLibCheck: true setting tsconfig.json10 skips type checking of .d.ts files in node_modules/. This:
  • Speeds up compilation significantly
  • Avoids errors from incompatible library types
  • Focuses type checking on project source code
The library declares peer dependencies for React 18/19 in package.json55-58 relying on consumer projects to provide compatible versions. Sources: tsconfig.json9-12 package.json30 package.json48-53 package.json55-58 package.json62

Integration with Build Scripts

The TypeScript configuration integrates with several build scripts defined in package.json. Build Script Integration Diagram

Build Script

The npm run build command package.json22 executes:
This:
  1. Reads all settings from tsconfig.json
  2. Compiles src/**/*.ts and src/**/*.tsx to dist/
  3. Generates .d.ts type definitions in dist/
  4. Applies path alias transformations
  5. Enables strict type checking
  6. Exits with code 1 if any type errors are found
The output is consumed by the NPM package distribution documented in Package Structure.

Type Check Script

The npm run type-check command package.json23 executes:
The --noEmit flag:
  • Performs full type checking without generating files
  • Validates types in local development
  • Runs faster than full compilation
  • Useful in CI/CD pipelines before deployment
This script verifies type safety without the overhead of emitting JavaScript.

Lint Script Integration

The npm run lint command package.json24 runs ESLint on TypeScript files:
ESLint respects the TypeScript configuration through:
  • Understanding path aliases defined in tsconfig.json
  • Parsing TypeScript syntax (.ts, .tsx extensions)
  • Accessing type information for advanced linting rules
The npm run lint:fix variant package.json25 automatically fixes fixable issues. Sources: package.json22-25 tsconfig.json1-26

Summary

The TypeScript configuration in @ui8kit/core establishes a modern, type-safe build pipeline:
  1. Compilation Target: ES2022 modules with bundler-friendly resolution
  2. Type Generation: Full declaration files (.d.ts) for IDE integration
  3. Strict Checking: All strict mode options enabled for type safety
  4. Path Aliases: @/* for internal imports, @ui8kit/core for package reference
  5. Output Structure: dist/ directory mirroring src/ structure
  6. JSON Support: Import registry metadata as typed modules
  7. Build Integration: Scripts for compilation, type checking, and linting
This configuration supports the three-layer architecture documented in Architecture while enabling multiple distribution methods described in Package Structure. Sources: tsconfig.json1-26 package.json22-25 package.json30-43