Set up ESLint in the eslint.config.js file to specifically scan TypeScript files with the extension *.ts

How can I configure the new "flat config" file (eslint.config.js) to instruct ESLint to exclusively analyze TypeScript files (*.ts) and apply the TypeScript "strict" rules?

eslint.config.js

import tseslint from 'typescript-eslint';

export default [
   ...tseslint.configs.strict,
   {
      files: ['**/*.ts'],
      rules: {
         '@typescript-eslint/no-non-null-assertion': 'off',
      },
   },
];

package.json

   "scripts": {
      “lint": "eslint --max-warnings 0"
   },

When I run the npm run lint command for the lint task, it fails because ESLint analyzes all JavaScript files in my project (resulting in numerous error messages).

My goal is for only TypeScript files (*.ts) to undergo analysis with the "strict" rules.

Bonus Inquiry:
While the official ESLint documentation provides detailed information on each configuration option, a comprehensive overview of file selection strategy seems lacking. The "flat config" consists of an array of objects, but what is the overall process flow for grouping and processing files through these objects?

Answer №1

For more information, you can visit the following links: and :

export default tseslint.config({
  files: ['**/*.ts'],
  extends: [
    eslint.configs.recommended,
    ...tseslint.configs.strict,
  ],
  rules: {
    '@typescript-eslint/no-non-null-assertion': 'off',
  },
});

When using ESLint flat configs, it's important to note that configuration objects are applied in order. Each config object will apply to all files being linted unless specified otherwise with properties like files. In this case, there are at least 2 configuration objects introduced:

  1. All of the tseslint.configs.strict objects, which by default apply to all configured files
  2. An object specifically for files: ['**/*.ts']

To ensure consistency, make sure to include { files: ['**/*.ts'] } for all settings using tseslint.config.

Answer №2

I found @Josh's answer to be helpful, but it only covered part of the solution. Another user provided a well-documented response at , explaining that eslint will include JavaScript files by default unless specifically excluded. When using tseslint.config, it is necessary to define exclusions in a separate configuration object.

Here is a suggestion based on the linked answer:

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  {
    ignores: [
      'dist/', // exclude specific folder
      '**/*.js', // exclude all JavaScript files
    ],
  },
  {
    files: ['**/*.ts'],
    extends: [
      eslint.configs.recommended,
      ...tseslint.configs.strict,
    ],
  },
);

Answer №3

Have you attempted this method without relying on tseslint.config?

import js from '@eslint/js'
import tsParser from '@typescript-eslint/parser'
import tsEslintPlugin from '@typescript-eslint/eslint-plugin'
import tseslint from 'typescript-eslint'

export default [
  ...tseslint.configs.strict,
  {
    files: ['**/*.{js,mjs,ts}']
  },
  {
    ignores: ['**/node_modules', 'dist/']
  },
  js.configs.recommended,
  {
    languageOptions: {
      parser: tsParser,
    },
    plugins: {
      '@typescript-eslint': tsEslintPlugin,
    },
    rules: {
      'no-undef': 'warn',
      'no-unused-vars': 'warn',
      semi: ['error', 'never']
    }
  }
]

Answer №4

Here is the solution that meets my specific requirements:

  1. Excludes linting of *.js files (which is crucial for my use case)
  2. Avoids the need for the tseslint.config() wrapper (as I rarely make changes to the configuration file and do not require IDE assistance)
  3. Maintains a clean and straightforward setup

eslint.config.js

import eslint   from '@eslint/js';
import tseslint from 'typescript-eslint';

export default [
   eslint.configs.recommended,
   ...tseslint.configs.strict,
   { ignores: ['**/*.js'] },
   {
      rules: {
         '@typescript-eslint/no-non-null-assertion': 'off',
      },
   },
];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 2: Implementing a Class Addition with a Delay

I'm attempting to animate a list of items in Angular 2, but for some reason, it's not working at all. What I'm doing is retrieving a HTMLCollection using getClass and then adding a class with a timeout. platform.ready().then((readySour ...

Integrate service once the constructor has completed execution

I created a service to connect with the Spotify API. When initializing this service in the constructor, it needs to retrieve the access token by subscribing to an Observable. However, there's an issue: The service is injected into my main component ...

Can anyone offer any suggestions for this issue with Angular? I've tried following a Mosh tutorial but it's

Just finished watching a video at around 1 hour and 35 minutes mark where they added the courses part. However, I encountered an error during compilation. ../src/app/app.component.html:2:1 - error NG8001: 'courses' is not recognized as an elemen ...

Angular8 with the [tinymce] library for customizing editor elements and configuring multiline options

I am currently working with Angular 8 and in the template, we have the following code snippet: <editor required class="research-modal__control__input research-modal__control__input__description" formCo ...

Block-level declarations are commonly used in TypeScript and Asp.net MVC 5

In my asp.net mvc5 project, I decided to incorporate TypeScript. I created an app.ts file and installed the nuget-package jquery.TypeScript.DefinitelyTyped. Here is a snippet of the app.ts code: /// <reference path="typings/jquery/jquery.d.ts"/> cl ...

The status of the Office.js appointment remains updated even after the saveAsync callback is executed

Utilizing the Office JavaScript API for an Outlook add-in, I encountered a issue with some code designed to save an appointment and close its window. Despite saving the appointment through the API, I continue to receive a "Discard changes" confirmation dia ...

Showing the date object in a react component

As a new programmer, I decided to start with React hooks based on recommendations that it's easier to learn. However, I encountered an issue when trying to display the deliveryDate on the screen. React doesn't seem to accept objects as children, ...

The 'split' property is not found on the 'Int32Array' type

ERROR located in src/app/account/phone-login/phone-login.component.ts(288,53): error TS2339: Property 'split' is not a valid property for type 'string | Int32Array'. Property 'split' cannot be found on type 'Int32Array& ...

Configure Jest to run tests for Lit using TypeScript with Microbundle

Today, I began the process of migrating our web components to lit-html. However, when attempting to run the initial test, an error message was encountered: SyntaxError: Cannot use import statement outside a module > 1 | import {LitElement, html, css, s ...

When using Vue to bind a data URI to the img src property, I noticed that the image in the browser only updates when there is

Currently, I am working on generating a base64 string to represent an image and passing that image data to a child component as part of an object property. The object class I am using for this purpose has an image property along with some other properties ...

The parameter type 'Function' cannot be assigned to the parameter type 'ComponentType<never>'

Having an issue passing a component to the connect method from react-redux. The error message I'm receiving is as follows: Argument of type 'Function' is not assignable to parameter of type 'ComponentType'. Type 'Function&ap ...

The upcoming router is not compatible with the NextPage type

I am currently working on introducing dynamic routing into an application that was originally designed with static routes. However, I am facing some confusion as I encounter TypeScript errors that are difficult for me to understand. Below is the code snipp ...

Creating a nested JSON file dynamically in Angular: A step-by-step guide

I am looking to dynamically generate a nested JSON file within an Angular project. The data will be extracted from another JSON file, with two nested loops used to read the information. Below is an example of the initial JSON file structure: { "data": [ ...

Arrange a JavaScript map based on its values and ensure that a specific field index remains at the top position

I'm sure this question may seem simple to some, but as a JavaScript novice, I couldn't find the answer myself. Here is the code snippet I'm working with: Let map = new Map<String,String> map.set('0', select) map.set('1&a ...

Modify the MUI time picker to display as a digital clock inside a DateTimePicker widget

I need to update my MUI DateTimePicker component to use the DigitalClock time picker instead of the Analog Clock picker. The current component has two steps, first picking the date from a calendar and then selecting the time. This change is only necessary ...

What method can be used to determine a number that is nonzero?

Defining the type of Result has presented some challenges for me. It should adhere to one of the following patterns: type Result<Some> = { code: 0; some_result: ...} // or type Result = { code: NonZero; message: string} How can I properly type this? ...

Looping through GET requests

I have a Next.js and TypeScript application where a request is made to a webhook integration that returns a Google sheet in JSON format. I've noticed that the integration keeps getting called repeatedly in a loop. Here is a snippet of my code: import ...

What is the best way to utilize ngForTemplate for wrapping a template?

Using the ngForTemplate in a ListView component for custom templates has proven to be challenging. Modified list-view.component.html: <div class="list-view"> <template ngFor [ngForOf]="items" [ngForTemplate]="template"> </template& ...

Lazy loading implemented with BootstrapVue's b-nav component

Having some trouble wrapping my head around the following issue: I've created a Vue.js component with tabs that have routes. I opted for a variation of the b-nav Tabs style (official docs) and it's functioning well in terms of tabs and routing. ...

Leveraging conditional types and optional variables in Typescript to translate a string into another form

Visit Playground I have been experimenting with creating a versatile function that can map one string to another during the compilation phase. The idea is simple - if a string is provided as input, it should return "data", and if the input is undefined, i ...