DEV Community

Patrick
Patrick

Posted on

tscw-config: Run tsc on files with tsconfig respected

If you've ever wanted to run tsc on specific files while applying all the CLI options from your tsconfig.json (which tsc doesn’t allow), I created a project called tscw-config that allows users to do just that:

  • It provides CLI and API that you can use.
  • It seamlessly integrates with most popular package managers, including:
    • npm
    • pnpm
    • Yarn
    • Yarn (Plug’n’Play)
  • It is well tested.

One common use case for running tsc on certain files is in a pre-commit hook. For example type-checking staged file when using lint-staged.

Here's an example of using it in a .lintstagedrc.js file. You can also check out the .lintstagedrc.mjs in tscw-config.

/**
 * Passing absolute path is fine, but relative path is cleaner in console.
 * @param {string[]} files
 */
const typeCheck = files => {
  const cwd = process.cwd();
  const relativePaths = files.map(file => path.relative(cwd, file)).join(" ");
  return `npx tscw --noEmit ${relativePaths}`;
};

export default {
  "**/*.{ts,mts,cts,tsx}": [prettier, typeCheck, eslint],
};
Enter fullscreen mode Exit fullscreen mode

Feel free to check it out and let me know if you have any feedback or suggestions!

Top comments (0)