Skip to content

Commit

Permalink
feat: support --noCheck for transpiling without type-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 24, 2024
1 parent a258a28 commit bea58a5
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 49 deletions.
19 changes: 11 additions & 8 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/isolated_typecheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")

ts_config(
name = "tsconfig",
src = "tsconfig.json",
visibility = [":__subpackages__"],
)
16 changes: 16 additions & 0 deletions examples/isolated_typecheck/backend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "backend",
declaration = True,
isolated_typecheck = True,
tsconfig = "//examples/isolated_typecheck:tsconfig",
deps = ["//examples/isolated_typecheck/core"],
)

assert_outputs(
name = "test_backend_default_outputs",
actual = "backend",
expected = ["examples/isolated_typecheck/backend/index.js"],
)
10 changes: 10 additions & 0 deletions examples/isolated_typecheck/backend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { IntersectionType } from '../core'

// Example object of IntersectionType
const myObject: IntersectionType = {
a: 42,
b: 'backend',
c: true,
}

console.log(myObject)
9 changes: 9 additions & 0 deletions examples/isolated_typecheck/core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "core",
declaration = True,
isolated_typecheck = True,
tsconfig = "//examples/isolated_typecheck:tsconfig",
visibility = ["//examples/isolated_typecheck:__subpackages__"],
)
20 changes: 20 additions & 0 deletions examples/isolated_typecheck/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* A file with some non-trivial types, so type-checking it may take some time.
* This helps to motivate the example: we'd like to be able to type-check the frontend and backend in parallel with this file.
*/
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never

// Example usage
type UnionType = { a: number } | { b: string } | { c: boolean }

export type IntersectionType = UnionToIntersection<UnionType>

export const MyIntersectingValue: IntersectionType = {
a: 1,
b: '2',
c: true,
}
13 changes: 13 additions & 0 deletions examples/isolated_typecheck/frontend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
name = "frontend",
isolated_typecheck = True,
tsconfig = {
"compilerOptions": {
"declaration": True,
"isolatedDeclarations": True,
},
},
deps = ["//examples/isolated_typecheck/core"],
)
13 changes: 13 additions & 0 deletions examples/isolated_typecheck/frontend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { IntersectionType } from '../core'
import { MyIntersectingValue } from '../core'

// Example object of IntersectionType
const myObject: IntersectionType = {
a: 42,
b: 'frontend',
c: true,
}

const otherObject = MyIntersectingValue

console.log(myObject, otherObject, myObject === otherObject)
8 changes: 8 additions & 0 deletions examples/isolated_typecheck/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"isolatedDeclarations": true,
"declaration": true
},
// Workaround https://github.com/microsoft/TypeScript/issues/59036
"exclude": []
}
6 changes: 6 additions & 0 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def ts_project(
assets = [],
extends = None,
allow_js = False,
isolated_typecheck = False,
declaration = False,
source_map = False,
declaration_map = False,
Expand Down Expand Up @@ -153,6 +154,10 @@ def ts_project(
See https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options
Typically useful arguments for debugging are `--listFiles` and `--listEmittedFiles`.
isolated_typecheck: Whether to type-check asynchronously as a separate bazel action.
Requires https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/#the---nocheck-option6
Requires https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#isolated-declarations
transpiler: A custom transpiler tool to run that produces the JavaScript outputs instead of `tsc`.
Under `--@aspect_rules_ts//ts:default_to_tsc_transpiler`, the default is to use `tsc` to produce
Expand Down Expand Up @@ -416,6 +421,7 @@ def ts_project(
incremental = incremental,
preserve_jsx = preserve_jsx,
composite = composite,
isolated_typecheck = isolated_typecheck,
declaration = declaration,
declaration_dir = declaration_dir,
source_map = source_map,
Expand Down
14 changes: 14 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
mandatory = True,
allow_single_file = [".json"],
),
"isolated_typecheck": attr.bool(
doc = """\
Whether type-checking should be a separate action.
This allows the transpilation action to run without waiting for typings from dependencies.
Requires a minimum version of typescript 5.6 for the [noCheck](https://www.typescriptlang.org/tsconfig#noCheck)
flag which is automatically set on the transpilation action when the typecheck action is isolated.
Requires [isolatedDeclarations](https://www.typescriptlang.org/tsconfig#isolatedDeclarations)
to be set so that declarations can be emitted without dependencies. The use of `isolatedDeclarations` may
require significant changes to your codebase and should be done as a pre-requisite to enabling `isolated_typecheck`.
""",
),
"validate": attr.bool(
doc = """whether to add a Validation Action to verify the other attributes match
settings in the tsconfig.json file""",
Expand Down
Loading

0 comments on commit bea58a5

Please sign in to comment.