question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build typescript on commit

See original GitHub issue

I’m trying to rebuild my typescript project on each commit, after prettifying the code with prettier:

"lint-staged": {
    "*.ts": [
      "prettier --parser typescript --write --no-semi",
      "tsc",
      "git add"
    ]
  },

Problem is that the tsc command here seems to fail to see my .tsconfig.json file, probably because the working directory of lint-staged is set to the directory of the file that is committed.

Is there a way to make this work? If I wanted to use something like tsc --rootDir ../.. to “fix” the working directory how would I know the level of depth to go up in the directory tree for the currently staged file?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

115reactions
szhucommented, Mar 27, 2020

hi, I know this is an already closed issue, but I still ran into it. I was able to fix it though!

I think the issue has nothing to do with tsconfig or the working directory. The issue is that lint-staged is appending the names of the changed files to the command. So if you’re telling it to run tsc, it’s actually running tsc changedfile1.ts changedfile2.ts. That’s why it’s ignoring your tsconfig, because passing extra files to it explicitly overrides that.

I fixed this issue with the following config:

  "lint-staged": {
     "*.ts": [
       "prettier --parser typescript --write --no-semi",
-      "tsc",
+      "bash -c tsc",
       "git add"
     ]
   },

bash doesn’t automatically pass its args down to child commands – bash -c tsc effectively runs tsc but ignores all args.

41reactions
sudo-suhascommented, Jul 5, 2018

Why would you want to use lint-staged for that? Just use husky pre-commit hook to run what ever command you need. It could be done using lint-staged && tsc-build-cmd. Note that tsc-build-cmd is not a valid command and needs to be substituted with the one which will build your project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Run a TypeScript type check in your pre-commit hook using ...
A great way to prevent TypeScript compilation errors from bringing down your CI pipelines is to introduce a type check before you commit...
Read more >
Configuring Prettier and TypeScript Compiler as a Pre-commit ...
Configuring Prettier and TypeScript Compiler as a Pre-commit Hook · 1. Install prettier, husky and lint-staged · 2. Configure prettier · 3. Create...
Read more >
Create a Pre-commit Git Hook to Check and Fix Your ...
In this post, we will introduce how to create a Git pre-commit hook to check JavaScript/TypeScript code using ESLint, Prettier, lint-staged, and Husky....
Read more >
How to check typescript type before commit? - Stack Overflow
It it, you can get the files about to be commited with git diff --staged --name-only you could then run your tsc --noEmit...
Read more >
Pre-Commit Hooks for Pretter and ESLint in TypeScript Projects
Learn how to configure your TypeScript project to automatically prettify and lint your code before committing to git using husky.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found