Build typescript on commit
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:11
- Comments:22 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 runtsc
, it’s actually runningtsc 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:
bash
doesn’t automatically pass its args down to child commands –bash -c tsc
effectively runstsc
but ignores all args.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 usinglint-staged && tsc-build-cmd
. Note thattsc-build-cmd
is not a valid command and needs to be substituted with the one which will build your project.