DEV Community

Cover image for Eslint the danger
md mamun
md mamun

Posted on

Eslint the danger

How to crack eslint for a typescript project

*Create a project with this command *

mkdir project-name
cd project-name
npm init -y # for shortcut if you use npm init than you have to modify yourself
Enter fullscreen mode Exit fullscreen mode

Install some indespensible packages

npm install express mongoose cors dotenv --save
npm install typescript @types/node @types/express --save-dev
npm install -D nodemon ts-node-dev eslint @eslint/js @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier
Enter fullscreen mode Exit fullscreen mode

Now create ts config file with this command

tsc --init
Enter fullscreen mode Exit fullscreen mode

Maintain folder structure like this

*Update you tsconfig.js file *

{
  "compilerOptions": {
"module": "commonjs",   
"rootDir": "./src",
"outDir": "./dist",   
"esModuleInterop": true, 
 "forceConsistentCasingInFileNames": true, 
 "strict": true, 
 "skipLibCheck": true                          
  },
  "include": ["src"]
}
Enter fullscreen mode Exit fullscreen mode

*For the last step you have to move eslint.config.mjs file
and paste this *

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

export default tseslint.config({
    files: ['**/*.ts'],
    extends: [
      eslint.configs.recommended,
      ...tseslint.configs.recommended,
    ],
    rules: {
      '@typescript-eslint/array-type': 'error',
      '@typescript-eslint/consistent-type-imports': 'error',
    },
  });
Enter fullscreen mode Exit fullscreen mode

If you have face import error solve like this

import type { Request, Response } from 'express';
import express from 'express';

const app = express();

app.use(express.json());

app.get('/', (req: Request, res: Response) => {
  res.send('Hello from setup file');
});

export default app;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)