Quickest Way to Add Eslint to JavaScript and TypeScript Projects

The quickest way to add eslint to your project is using eslint init command. ./node_modules/.bin/eslint –init will create eslintrc.js and install dependencies automatically.

(1) Getting started for JavaScript Application

It is super simple. Just install eslint, use eslint tool with a init flag and follow the command line instruction.

npm i --save-dev eslint
./node_modules/.bin/eslint --init

Then, add scripts to package.json.

"lint": "./node_modules/.bin/eslint ./",
"lint-fixup": "./node_modules/.bin/eslint ./ --ext .js,.jsx --fix"

(2) Getting started for TypeScript Application

The same as JavaScript. Note that eslint init will install @typescript-eslint/eslint-plugin @typescript-eslint/parser when you choose yes to typescript option. However the tool uses npm. If you are using yarn it’s better to install it before init.

# install dependencies
yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

# Choose TypeScript as an option when we go through the command line tool.
./node_modules/.bin/eslint --init

Here is an example of eslintrc.js created by the command-line tool.

module.exports = {
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
    }
};
Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …