Skip to content

Commit 20f90c5

Browse files
committed
fix(typescript): document tsc_test for typecheck-only
1 parent 5852aa7 commit 20f90c5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

packages/typescript/index.docs.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ Then call it, using the [`npm_package_bin`](Built-ins#npm_package_bin) documenta
5151
Here is an example:
5252
https://github.com/bazelbuild/rules_nodejs/blob/3.2.2/internal/node/test/BUILD.bazel#L491-L507
5353
54+
### tsc_test
55+
56+
`tsc_test` is generated alongside `tsc`.
57+
It is identical, except that Bazel treats it as a test target, producing only an exit code
58+
rather than files to be consumed by other steps in the build.
59+
60+
This can be used for a build with `--noEmit`, so that TypeScript is purely used for
61+
type-checking and not for producing any build outputs.
62+
63+
To use it, add the load statement `load("@npm//typescript:index.bzl", "tsc_test")` to your BUILD file.
64+
(Possibly replacing `@npm` with the name of the repository where you installed dependencies)
65+
66+
See example in https://github.com/bazelbuild/rules_nodejs/tree/stable/packages/typescript/test/tsc_test
67+
5468
### ts_project
5569
5670
`ts_project` simply runs `tsc --project`, with Bazel knowing which outputs to expect based on the TypeScript compiler options,
@@ -150,6 +164,10 @@ your editor references, or `extends` from it, to keep consistent settings for th
150164
151165
Anything you do with TypeScript is possible with `ts_project`, including json imports, type-checking only,
152166
transpile only, outdir, rootdir, and so on.
167+
168+
> To use `ts_project` for typecheck-only, you'll still need to use --declaration so that .d.ts files are produced.
169+
> Alternatively, see the `tsc_test` rule documented above.
170+
153171
See many examples in our test cases:
154172
https://github.com/bazelbuild/rules_nodejs/tree/stable/packages/typescript/test/ts_project
155173
"""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@npm//typescript:index.bzl", "tsc_test")
2+
3+
# Bazel only runs actions as required to produce requested outputs.
4+
# If we don't want TypeScript to emit anything at all, even .d.ts files,
5+
# then we can't make it a build step as it will never run.
6+
# Instead we invoke the bare `tsc` binary as a *_test rule, so that Bazel
7+
# always runs it to produce an exit code.
8+
tsc_test(
9+
name = "typecheck_only",
10+
args = ["$(location check_me.ts)"],
11+
data = ["check_me.ts"],
12+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const a: string = 'a'
2+
3+
// Uncomment this last line to observe the typecheck_only test fails:
4+
// FAIL: //packages/typescript/test/tsc_test:typecheck_only
5+
// (see execroot/build_bazel_rules_nodejs/bazel-out/k8-fastbuild/testlogs/packages/typescript/test/tsc_test/typecheck_only/test.log)
6+
// INFO: From Testing //packages/typescript/test/tsc_test:typecheck_only:
7+
// ==================== Test output for //packages/typescript/test/tsc_test:typecheck_only:
8+
// packages/typescript/test/tsc_test/check_me.ts(11,14): error TS2322: Type 'string' is not assignable to type 'number'.
9+
// ================================================================================
10+
11+
// export const b: number = 'b'

0 commit comments

Comments
 (0)