Skip to content

Commit d68e8bb

Browse files
authored
Add TypeScript typechecking and configure for CI (#4606)
1 parent 0cd0b4b commit d68e8bb

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

.github/workflows/validate.yml

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ jobs:
1515
cache: 'npm'
1616
cache-dependency-path: './package-lock.json'
1717
- run: 'npm clean-install'
18+
- run: 'npm run typecheck'
1819
- run: 'npm run eslint'
1920
- run: 'node ./cli.js check'

cli.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import fsCb from 'node:fs'
66
import readline from 'node:readline'
77
import util from 'node:util'
88

9-
import AjvDraft04 from 'ajv-draft-04'
9+
import _AjvDraft04 from 'ajv-draft-04'
1010
import { Ajv as AjvDraft06And07 } from 'ajv'
11-
import Ajv2019 from 'ajv/dist/2019.js'
12-
import Ajv2020 from 'ajv/dist/2020.js'
13-
import addFormats from 'ajv-formats'
11+
import _Ajv2019 from 'ajv/dist/2019.js'
12+
import _Ajv2020 from 'ajv/dist/2020.js'
13+
import _addFormats from 'ajv-formats'
1414
import { ajvFormatsDraft2019 } from '@hyperupcall/ajv-formats-draft2019'
1515
import schemasafe from '@exodus/schemasafe'
1616
import TOML from 'smol-toml'
@@ -20,12 +20,30 @@ import * as jsoncParser from 'jsonc-parser'
2020
import ora from 'ora'
2121
import chalk from 'chalk'
2222
import minimist from 'minimist'
23-
import fetch from 'node-fetch'
23+
import fetch, { FetchError } from 'node-fetch'
2424

2525
/**
2626
* @import { Ora } from 'ora'
2727
*/
2828

29+
/**
30+
* Ajv defines types, but they don't work when importing the library with
31+
* ESM syntax. Tweaking `jsconfig.json` with `esModuleInterop` didn't seem
32+
* to fix things, so manually set the types with a cast. This issue is
33+
* tracked upstream at https://github.com/ajv-validator/ajv/issues/2132.
34+
*/
35+
/** @type {typeof _AjvDraft04.default} */
36+
const AjvDraft04 = /** @type {any} */ (_AjvDraft04)
37+
38+
/** @type {typeof _Ajv2019.default} */
39+
const Ajv2019 = /** @type {any} */ (_Ajv2019)
40+
41+
/** @type {typeof _Ajv2020.default} */
42+
const Ajv2020 = /** @type {any} */ (_Ajv2020)
43+
44+
/** @type {typeof _addFormats.default} */
45+
const addFormats = /** @type {any} */ (_addFormats)
46+
2947
// Declare constants.
3048
const AjvDraft06SchemaJson = await readJsonFile(
3149
'node_modules/ajv/dist/refs/json-schema-draft-06.json',
@@ -843,7 +861,9 @@ async function taskMaintenance() {
843861
`NOT OK (${res.status}/${res.statusText}): ${url} (after 405 code)`,
844862
)
845863
} catch (err) {
846-
console.info(`NOT OK (${err.code}): ${url} (after 405 code)`)
864+
console.info(
865+
`NOT OK (${/** @type {FetchError} */ (err).code}): ${url} (after 405 code)`,
866+
)
847867
}
848868
return
849869
}

jsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2+
"include": ["cli.js"],
23
"compilerOptions": {
34
"target": "es2022",
4-
"module": "Node16",
5-
"moduleResolution": "Node16",
5+
"module": "node16",
6+
"moduleResolution": "node16",
67
"strict": true
78
}
89
}

package-lock.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"scripts": {
2020
"prettier": "prettier --config .prettierrc.cjs --ignore-path .gitignore --check .",
2121
"prettier:fix": "prettier --config .prettierrc.cjs --ignore-path .gitignore --write .",
22+
"typecheck": "tsc --project ./jsconfig.json",
2223
"eslint": "eslint ./cli.js",
2324
"eslint:fix": "eslint --fix ./cli.js",
2425
"build": "echo \"WARNING: Please use 'node ./cli.js' instead of 'npm run'. This method for execution will be removed.\" && npm run eslint && node ./cli.js check && ./scripts/dirty_repository_check.sh"
@@ -28,6 +29,7 @@
2829
"@exodus/schemasafe": "^1.3.0",
2930
"@hyperupcall/ajv-formats-draft2019": "^1.7.2",
3031
"@prantlf/jsonlint": "^16.0.0",
32+
"@types/node": "^22.13.11",
3133
"ajv": "^8.17.1",
3234
"ajv-draft-04": "^1.0.0",
3335
"ajv-formats": "^2.1.1",
@@ -45,6 +47,7 @@
4547
"prettier-plugin-sort-json": "^4.0.0",
4648
"prettier-plugin-toml": "^2.0.1",
4749
"smol-toml": "^1.3.1",
50+
"typescript": "^5.8.2",
4851
"yaml": "^2.6.1"
4952
}
5053
}

0 commit comments

Comments
 (0)