ESLint Plugin for TypeScript Browser Compatibility

1.0.2 · active · verified Sun Apr 19

`eslint-plugin-typescript-compat` is an ESLint plugin designed to ensure browser compatibility for TypeScript code by identifying the usage of ECMAScript APIs that are not supported by your configured target browsers. Currently at version 1.0.2, the plugin integrates with `mdn-browser-compat-data`, the TypeScript Compiler API, and `browserslist` to perform static analysis. Unlike `eslint-plugin-compat`, which focuses on JavaScript, this plugin specifically leverages TypeScript's type information to provide more accurate linting for TypeScript projects. It differentiates itself from `eslint-plugin-es` and `eslint-plugin-es-x` by supporting the detection of prototype and static methods (e.g., `Array.prototype.find`, `Array.from`), not just language syntax features. The plugin primarily supports JavaScript Built-in Objects and their methods, with future plans to expand to DOM API compatibility. Its release cadence appears to be driven by feature additions and bug fixes, with a focus on stability for its current scope. Users must configure `parserOptions.project` and `tsconfig.json` `lib` settings for proper functionality.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the installation and configuration of `eslint-plugin-typescript-compat` across `package.json`, `.eslintrc.json`, and `tsconfig.json`, then provides TypeScript code that will trigger browser compatibility warnings for features unsupported in Internet Explorer 11, based on the `browserslist` configuration.

/*
1. Install dependencies:
   npm install eslint-plugin-typescript-compat typescript @typescript-eslint/parser --save-dev

2. Configure your .eslintrc.json:
*/
// .eslintrc.json
/*
{
  "extends": ["plugin:typescript-compat/recommended"],
  "env": {
    "browser": true
  },
  "parserOptions": {
    "project": "./tsconfig.json"
  }
}
*/

/*
3. Configure your tsconfig.json to include a suitable 'lib' array:
*/
// tsconfig.json
/*
{
  "compilerOptions": {
    "target": "es5", // Or your desired target
    "lib": ["ESNext", "DOM"], // Must include ESNext to recognize modern features for linting
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"]
}
*/

/*
4. Configure your target browsers using browserslist in package.json:
*/
// package.json
/*
{
  "name": "my-project",
  "version": "1.0.0",
  "browserslist": [
    "ie 11",
    "last 2 versions",
    "> 0.2%"
  ]
}
*/

/*
5. Create an example TypeScript file (e.g., src/index.ts):
   Running ESLint on this file with the above config will flag features unsupported in IE 11.
*/

const items: number[] = [1, 2, 3];

// Array.prototype.includes is not supported in IE 11. Will trigger a warning.
items.includes(2);

// Array.prototype.find is also not supported in IE 11. Will trigger a warning.
const found = items.find((item) => item === 2);

// Math.sign is not supported in IE 11. Will trigger a warning.
const s = Math.sign(-5);

console.log(items, found, s);

view raw JSON →