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.

lint-staged ignores tsconfig.json when it called through husky hooks

See original GitHub issue

Description

lint-staged ignores tsconfig.json when it called through husky hooks (see “Steps to reproduce”)

Steps to reproduce

Create these files (test.js, tsconfig, package.json):

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"
    ]
  }
}

Then on command line write:

// 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

Environment

  • OS: macOS Catalina
  • Node.js: v13
  • lint-staged: v10.1.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:21

github_iconTop GitHub Comments

81reactions
sombreroEnPuntascommented, Aug 16, 2020

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 😛

// 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"]
}
68reactions
antoinerousseaucommented, Apr 27, 2020

It’s because you get the filenames passed as an argument. Try using the function syntax. This is what I use:

// lint-staged.config.js
module.exports = {
  "*.{js,jsx}": [
    "eslint --cache --fix",
  ],
  "*.{ts,tsx}": [
    () => "tsc --skipLibCheck --noEmit", 
    "eslint --cache --fix",
  ],
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Run a TypeScript type check in your pre-commit hook using ...
Run a TypeScript type check in your pre-commit hook using lint-staged + husky ... A great way to prevent TypeScript compilation errors from ......
Read more >
Husky/lint-staged is it possible to exclude/ignore file?
While configuring lint-staged in package.json or If you're using any other IDE, in order to ignore/exclude files by lint-Staged and husky ...
Read more >
Lint on Commit | David Valles
The issue resides in how lint-staged calls tsc . It passes git staged files to tsc via the command line, which means tsc...
Read more >
Husky + Lint-Staged on a React TypeScript Project
To trigger some action before committing, we will use the Git Hook called “pre-commit”. Let's now create an action to execute our 'test'...
Read more >
lint-staged - npm
The concept of lint-staged is to run configured linter tasks (or other tasks) on files that are staged in git. lint-staged will always...
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