Turn on transpile-free type hinting for your vanilla JS projects #JSWithTypes
- Create a plain JavaScript project
npm init
- Turn on type linting πͺ
# Create a properly configured `jsconfig.json` npx jswt init
- Profit!
Works with VS Code out-of-the-box, and Vim + ale.
Your project will look something like this:
.
βββ bin/
βββ jsconfig.json
βββ node_modules/
β βββ @types/
β βββ node/
βββ package-lock.json
βββ package.json
βββ README.md
βββ types.js
βββ typings/
βββ overrides/
βββ index.d.ts
- Runs
tsc --init
with these options:npx -p [email protected] -- \ tsc --init \ --allowJs --alwaysStrict --checkJs \ --moduleResolution node \ --noEmit --noImplicitAny --target es2022 \ --typeRoots './typings,./node_modules/@types'
- Adds the following keys:
"include": ["*.js", "bin/**/*.js", "lib/**/*.js", "src/**/*.js"]` "exclude": ["node_modules"]
- Renames
tsconfig.json
tojsconfig.json
(and creates some placeholder files and dirs)
You may wish to add common script commands for fmt
and lint
:
npm pkg set scripts.lint="npx -p [email protected] -- tsc -p ./jsconfig.json"
npm pkg set scripts.fmt="npx -p [email protected] -- prettier -w '**/*.{js,md}'"
It should Just Workβ’, but if your vim setup is a little custom, you may want to add or modify a line like this:
~/.vimrc
:
let g:ale_linters = {
\ 'javascript': ['tsserver', 'jshint'],
\ 'json': ['fixjson']
\}