网页 综合 更新时间 2026-11-16 08:41:15

Type checking | Bun Docs

自动蜘蛛 bun.sh 正文约 4930 字
Type checking | Bun Docs
File & Module System
Standards & Compatibility
Type check your TypeScript project with bun check
bun check type checks your TypeScript project. It reads your tsconfig.json, reports the same errors as tsc from TypeScript 7, and uses every CPU core.
bun check exits with code 1 if it finds an error and 0 if it finds none. It never writes files.
To type check and then run, build, or test in one command, use the --check flag.
bun init does all of this for you. In an existing project, follow these steps.
Install Bun's type declarations
@types/bun declares what Bun provides: console, fetch, Bun, and modules such as bun:test.
List them in tsconfig.json
Since TypeScript 6.0, a package in node_modules/@types counts only if types lists it. List every package your code relies on, for example ["bun", "react"].
You don't need a tsconfig.json. Without one, bun check uses default compiler options and includes @types/bun if you have installed it.
You don't need the typescript package either. The declarations of Array, Promise, the DOM, and the rest of TypeScript's lib.*.d.ts files are built into Bun.
Your editor doesn't use bun check. It runs its own copy of TypeScript. To keep the two in agreement, install the version of TypeScript that bun check matches.
bun check reads the same tsconfig.json as tsc and checks the same files, so most projects only change the command.
Keep tsc to generate .d.ts files. bun check only checks.
bun check behaves like TypeScript 7, whichever version of typescript you have installed. TypeScript 6 and 7 changed some defaults and removed some compiler options.
Three defaults changed:
If tsconfig.json sets a removed option, bun check reports that option and checks no files until you fix it. tsc does the same.
See TypeScript 6 and 7 for the tsconfig.json that Bun recommends.
Install your dependencies, then run bun check. A type error fails the job.
If your projects are connected by references, run bun check -b to check all of them.
In GitHub Actions, bun check also prints a workflow command for each error. GitHub shows those errors as annotations on the lines of the pull request diff. You don't need a problem matcher.
Use the oven/bun image, or install Bun in an earlier step.
To check types and run tests in one step, use bun test --check. Bun runs the tests only if the test files, and everything they import, have no type errors.
Run bun check from a Git hook to catch type errors before they reach CI.
With lint-staged, check only the files you staged and the files they import:
lint-staged adds the staged file names to the command, as in bun check src/a.ts src/b.ts. bun check still uses your tsconfig.json for those files. tsc does not load tsconfig.json when you pass it file names.
A change to one file can break a file that imports it. Checking only staged files misses that error, so run bun check on the whole project in CI as well.
Without arguments, bun check looks for tsconfig.json in the current directory, then in each parent directory. It checks the files that files, include, and exclude select.
Use -p with a tsconfig.json or the directory that contains one.
Pass files or directories to check only those files and what they import.
Bun checks each file with the same tsconfig.json your editor uses for it:
To report only the errors of the files you name, and not those of the files they import, add --no-follow-imports. Bun loads the whole project, so each file gets the errors that a check of the project reports in it. It computes the types of the project's other files only where a named file needs them. Projects in references are still checked in full, because the named files read their declarations.
With --no-follow-imports, each path must be a file, and the project must include it or import it. If the nearest tsconfig.json has only references, a referenced project must include it. Anything else is an error, and so is an empty list of files. Errors in tsconfig.json and errors about the program, such as a missing global type or a /// <reference> to a file that doesn't exist, are always reported.
A directory means the files of the project inside that directory. If the project has no files there, Bun checks every file in the directory that exclude does not rule out. An example is bun check scripts when include is ["src"].
Bun uses the compiler options that bun init writes, without the three optional strictness rules noUncheckedIndexedAccess, noImplicitOverride, and noFallthroughCasesInSwitch.
bun check checks the project that tsc checks in the same directory: the one with the nearest tsconfig.json.
Run bun check in the package, or name it from anywhere.
If the package has references, Bun reads the referenced projects from their source files. You don't have to build them first, and Bun does not write .d.ts or .tsbuildinfo files. Bun reports the errors of the package only. Compiler options on the command line apply to the package only.
访问原文 返回搜索 https://bun.sh/docs/runtime/check
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。