evanw/esbuild

Question: use esbuild watch and typescript tsc --noEmit to check types on change

jpbnetley opened this issue · 2 comments

I would like to know if its possible to bundle the typescript files and check that typescript is happy with the types using esbuild's watch mode.

The current implementation I have is using node to read a js file used for building with esbuild.
I have a esbuild watch js file, that sets up the watch context with the passed in config.

But I am not sure how I can run that file, and also check the types at the same time.

I would like to have a solution where it bundles the files with esbuild, and also checks the typescript types during file change.

Is there a native way to do this with esbuild?

I know I can use something like concurrently to run the watch command for esbuild and tsc separately.
But I want to see if its possible to avoid installing an additional package.
And do type checking as files change.

build command

node ./esbuild.watch.config.mjs

esbuild.watch.config.mjs

import { context } from 'esbuild'
import { config } from './esbuild.shared.config.mjs'

async function runWatch() {
 const ctx = await context(config)
 await ctx.watch()
 console.log('Watching...');
}

runWatch()

esbuild.shared.config.mjs

/**
 * @type import('esbuild').BuildOptions
 */
export const config = {
  entryPoints: ['src/index.mts'],
  bundle: true,
  platform: 'node',
  target: "node18",
  outdir: "dist",
  allowOverwrite: true
}

src to pr: https://github.com/jpbnetley/test-ts-monorepo/pull/8/files

esbuild doesn't have a type checker, all it does is just to strip types and working on js sources. So if you want to do type checking it must involves the tsc. The only difference is how you run the tsc.

For minimum setup I would just recommend you to do nothing about executing tsc since modern editors (like VS Code or subl with lsp-typescript) already does type checking on you editing each file. To prevent the user from accidentally pushing code with type errors I can add a simple git commit hook which runs tsc --noEmit.

You can also start a tsc --watch --noEmit in a separate process.

Thanks for the suggestions, I'll try one of those.