@uxfront/typescript-config
v0.2.1
Published
Readme
@uxfront/typescript-config
Shared TypeScript configuration for uxfront projects. It type-checks strictly and leaves the output to your bundler, so every project that extends it catches the same mistakes and compiles the same way. For a library that ships its own declaration files, library.json turns emit back on.
Install
pnpm add -D typescript @uxfront/typescript-configUsage
Create a tsconfig.json that extends the shared config and lists your own files:
{
"extends": "@uxfront/typescript-config/tsconfig.json",
"include": ["src"]
}Keep the include: TypeScript resolves an extended config's include from the folder that config lives in, so without your own it looks for files inside node_modules/@uxfront/typescript-config and stops with TS18003: No inputs were found. Override any option under compilerOptions in the same file.
What it sets
Let's start with strictness, which is where you'll notice the config most:
| Option | Value | What it does for you |
| -------------------------- | ------ | --------------------------------------------------------------------------------------- |
| strict | true | Turns on every strict check, like strictNullChecks and noImplicitAny |
| noUncheckedIndexedAccess | true | Types items[0] and record[key] as possibly undefined, so you check before you use |
| noImplicitOverride | true | Asks for override on a method that replaces one from its base class |
Modules are written the way a bundler reads them:
| Option | Value | What it does for you |
| ---------------------- | ------------ | --------------------------------------------------------------------------------------- |
| module | "preserve" | Keeps import and export as you wrote them, and resolves imports the way bundlers do |
| verbatimModuleSyntax | true | Asks for import type on an import that only brings in types |
| isolatedModules | true | Flags code that a tool compiling one file at a time can't compile |
| moduleDetection | "force" | Treats every file as a module, even one without import or export |
| esModuleInterop | true | Lets you default-import a CommonJS package |
| resolveJsonModule | true | Lets you import a .json file, typed from its contents |
| allowJs | true | Includes .js files in the project |
And the target and output:
| Option | Value | What it does for you |
| -------------- | ----------------------------------- | -------------------------------------------------------------------- |
| target | "es2022" | Leaves ES2022 syntax, like class fields and top-level await, as is |
| lib | ["es2022", "dom", "dom.iterable"] | Types the ES2022 built-ins and the browser DOM |
| noEmit | true | Type-checks only and writes no files |
| skipLibCheck | true | Skips type-checking the .d.ts files of your dependencies |
Two of these are worth a second look:
verbatimModuleSyntaxfails the type-check onimport { Foo } from "./types"whenFoois only a type, withTS1484. Writeimport type { Foo } from "./types"instead.@uxfront/oxlint-confighas the matchingtypescript/consistent-type-importsrule, so your editor flags it as you type.noEmitmeanstscnever writes files. A bundler like Vite or a library builder like tsdown compiles your code, andtsconly checks it.
For a library that ships its own declaration files, extend the library variant instead, which turns emit back on.
Library variant
Let's look at library.json, for a package that ships declaration files, like one that tsc builds or one that a bundler builds and tsc describes. It extends tsconfig.json, so you keep every check above, and sets four compiler options:
| Option | Value | What it does for you |
| ------------- | --------------------- | ------------------------------------------------------------------------------------------ |
| noEmit | false | Lets tsc write files again |
| declaration | true | Writes a .d.ts file for each source file |
| composite | true | Lets other projects point at this one through references, and rebuilds only what changed |
| outDir | "${configDir}/dist" | Writes the output to your own project's dist folder |
Extend it the same way, with your own include:
{
"extends": "@uxfront/typescript-config/library.json",
"include": ["src"]
}Running tsc -p . then writes dist/src/index.js, dist/src/index.d.ts and dist/tsconfig.tsbuildinfo. The variant sets no rootDir, so your include can also cover files beside src, like vite.config.ts, and the output keeps the src folder. For dist/index.js, add "rootDir": "src" under compilerOptions, and add tsconfig.tsbuildinfo to your .gitignore, since TypeScript then writes it beside your tsconfig.json. To leave the JavaScript to your bundler, add "emitDeclarationOnly": true.
A few things are worth knowing before your first build:
outDiris your project'sdist.${configDir}stands for the folder of your owntsconfig.json. A plain"dist"would resolve next tolibrary.json, insidenode_modules.${configDir}needs TypeScript 5.5 or later.compositeneeds every file in yourinclude. It writes a.tsbuildinfofile, so the next build only redoes what changed. A file that's imported but not inincludestops the build withTS6307.- It excludes your
node_modulesanddist. That way a second build doesn't read the first one's output as input, which stops it withTS5055: Cannot write file … because it would overwrite input file. Your ownexcludereplaces this one, so if you set one, keep"${configDir}/dist"in it.
