Setting up type checking
Contents
Type checking is useful to catch errors early in the development process. This chapter will explain how to set up type checking for your project.
Configure
package.json
Add the following scripts to the package.json file:
{
"scripts": {
"check": "concurrently -c auto -g --timings npm:check:types",
"check:types": "concurrently -g -r --timings npm:check:types:*",
"check:types:client": "tsc --noEmit -p src/main/resources/assets/tsconfig.json",
"check:types:server": "tsc --noEmit -p src/main/resources/tsconfig.json",
}
}
Type checking is performed by TypeScript 7 — the native compiler. It only checks types here: the actual transpilation of both server-side and client-side code is done by tsdown, so the compilation targets (ES2015/CommonJS for the server, ES2015/ESM for the client) are unaffected by the TypeScript version. Note that the rest of the toolchain (Jest via @swc/jest, linting via oxlint) is chosen to not depend on the TypeScript JavaScript compiler API, which the native compiler no longer provides.
build.gradle
Add the following task to the build.gradle file, which will run the npm check script defined in package.json:
tasks.register('npmCheck', NpmTask) {
dependsOn npmInstall
args = [
'run',
'check'
]
environment = [
'FORCE_COLOR': 'true',
]
}
check.dependsOn npmCheck
The last command makes sure that type checking is executed as a part of the build, but it can also be run separately (to skip the entire build process and just check for errors) with the following command:
./gradlew check