eslint-plugin-node icon indicating copy to clipboard operation
eslint-plugin-node copied to clipboard

Consider adding typescript faq, recommended-typescript config

Open estaub opened this issue 4 years ago • 5 comments

This is a reopening of https://github.com/mysticatea/eslint-plugin-node/issues/205.

Typescript is usually (IMHO) configured, when used with node, to translate what look like ECMA imports and exports down to CommonJS. I know that's evolving, but only the brave and those without other dependencies are going all in. In this config, what look like ECMA imports and exports in the .ts source really aren't.

The plugin:node/recommended-typescript-commonjs config should probably include something like:

   "rules": {
        "node/no-unsupported-features/es-syntax": [
            "error",
            {
                "ignores": [
                    "modules"
                ]
            }
        ],
        "node/no-missing-import": ["error", {
            "tryExtensions": [".js", ".ts"]
        }]
    }

It probably makes sense to also soon have another plugin:node/recommended-typescript-esm for when typescript is set up to emit ecmascript modules.

Also, it would be helpful if the doc specified whether node should come before or after typescript in extends.

estaub avatar Jul 11 '20 16:07 estaub

node/no-missing-import would also need to take into account import type

Sparticuz avatar Aug 11 '20 15:08 Sparticuz

".d.ts" will be needed as well in node/no-missing-import - tryExtensions

ZYinMD avatar Sep 21 '20 02:09 ZYinMD

I've also run into a couple issues with TypeScript.

// index.ts
import {RequestHandler} from 'express' // node/no-missing-import and node/no-unpublished-import reported

export function logger(): RequestHandler {
  return (req, res, next) => {
    //...
  }
}
// package.json
{
  "dependencies": {
    "@types/express": "*"
  },
  "devDependencies": {
    "express": "*"
  }
}

node/no-missing-import reports an error when only @types/<module> is needed. In the example above, RequestHandler is an interface from @types/express, but there is no need to add a dependency for express since the package never needs to import any Express code.

node/no-unpublished-import reports an error in a similar way when a module is only needed as a devDependency (e.g. for testing) but only its types are needed for production, and those types come from @types/<module>.

I've turned off node/no-missing-import since TypeScript checks this for me, but node/no-unpublished-import is not something TypeScript checks, so it would be nice if eslint-plugin-node could do it while supporting TypeScript.

jordanbtucker avatar Oct 05 '21 21:10 jordanbtucker

Is there any place that talks about this? I'm trying to sort this out in https://github.com/snowpackjs/prettier-plugin-astro/pull/56/ and I'd love for it to work! Would it be possible to make a community-based recommended config?

jasikpark avatar Nov 08 '21 21:11 jasikpark

here are a couple of commented configs for eslint and tsconfig (eslintrc.json is fine with comments):

eslintrc.json

{
  "env": {
    /* safely matches tsconfig target, even if tsc might backport/shim */
    "es2020": true,
    "node": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:node/recommended",
    "plugin:import/recommended",
    "plugin:import/typescript",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint", "import", "node"],
  /* not really necessary, just safety */
  "root": true,
  "rules": {
    /* allow import/export syntax, tied to package.json engine.node values */
    "node/no-unsupported-features/es-syntax": [
      "error",
      { "ignores": ["modules"] }
    ],
    /* let the import plugin deal with module checking */
    "node/no-missing-import": "off"
  },
  "settings": {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", "tsx"]
    },
    "import/resolver": {
      "typescript": { "alwaysTryTypes": true }
    }
  }
}

tsconfig.json

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "include": ["./src"],
  "compilerOptions": {
    "outDir": "./dist"
  }
}

barraponto avatar Apr 27 '22 20:04 barraponto