lint-staged ignores tsconfig.json when it called through husky hooks (see "Steps to reproduce")
test.ts
export class Test {
static get observedAttributes() {
return ['foo'];
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ESNEXT"
}
}
package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,ts}": [
"tsc --noEmit"
]
}
}
// everything works fine (it uses tsconfig.json)!
tsc --noEmit
// throw an error because it ignores tsconfig.json
git commit // error TS1056: Accessors are only available when targeting ECMAScript 5 and higher
lint-staged: v10.1.0Can you try explicitly setting the config file for tsc?
"lint-staged": {
"*.{js,ts}": [
"tsc -p tsconfig.json --noEmit"
]
}
We have a couple of issues like this, and I can't say for certain what causes it. I assume it might be a wrong working directory.
Also, please post your debug logs by enabling them:
"husky": {
"hooks": {
"pre-commit": "lint-staged --debug"
}
},
Do you by chance have tsc installed locally in the project, or globally? This might be something that affects it.
It's because you get the filenames passed as an argument. Try using the function syntax.
This is what I use:
```js
// lint-staged.config.js
module.exports = {
".{js,jsx}": [
"eslint --cache --fix",
],
".{ts,tsx}": [
() => "tsc --skipLibCheck --noEmit",
"eslint --cache --fix",
],
}
Similar issue with tsc.
Passing files AND project config is not possible.
When invoked from husky, it does not find the project folder or configuration :$
When invoked from bash, it finds the project folder... but the config prevents passing files as args.
Here's my setup. It works as long as all changes are staged :P
// package.json
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"yarn eslint --quiet --fix",
"bash -c tsc --noEmit" // notice bash!
]
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "lint-staged"
}
},
// tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
// notice the filters!
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
same issue
@sombreroEnPuntas thank you! it's working for me
adding bash fixed it for me, thanks @sombreroEnPuntas
"*.{ts,tsx}": [
"npm run lint",
"bash -c 'npm run check-types'"
],
@antoinerousseau thanks! using it in a js file with a function syntax works.
Hardcoding bash is not really a good solution for crossplatform development.
Thank you all 馃憤
Seems to have been solved. If someone wants to open a PR about adding this to the README, we'd appreciate it!
Most helpful comment
Similar issue with
tsc.Passing files AND project config is not possible.
When invoked from husky, it does not find the project folder or configuration :$
When invoked from bash, it finds the project folder... but the config prevents passing files as args.
Here's my setup. It works as long as all changes are staged :P