首页 > 解决方案 > 需要 javascript 和 typescript 文件才能在 nodejs express 应用程序上工作

问题描述

慢慢地将 nodejs express javascript 应用程序转换为 typescript 应用程序。然而,由于 nodejs 使用 require/module.exports 和 typescript 使用 import/exports,eslint 经常让我头疼,因为报告 require/module.exports 是 eslint(no-undef)。我不想禁用 no-undef。

tsconfig.json

"compilerOptions": {
        /* Visit https://aka.ms/tsconfig.json to read more about this file */
        /* Basic Options */
        // "incremental": true,                   /* Enable incremental compilation */
        "target": "ES2020",
        "module": "commonjs",
        "lib": [
            "DOM",
            "ESNext"
        ],
        "declaration": false,
        "sourceMap": true,
        "outDir": "./dist", 
        "rootDir": ".",
        "composite": false, 
        "removeComments": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "moduleResolution": "node",
        "baseUrl": ".",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        /* -------------Revert when possible------------- */
        "noImplicitAny": false /* "noImplicitAny": true, Raise error on expressions and declarations with an implied 'any' type. */
    },
    "exclude": [
        "node_modules",
        "./dist"
    ],
    "include": [
        // "./server/**/*.tsx",
        "./server/**/*.ts"
    ]
}

.eslintrc.js

module.exports = {
    root: true,
    parser: "@typescript-eslint/parser",
    env: {
        node: true,
    },
    parserOptions: {
        ecmaVersion: 2020,
        sourceType: "module",
    },
    extends: [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "prettier/@typescript-eslint",
        "plugin:prettier/recommended",
        "plugin:node/recommended-module",
    ],
    rules: {
        "prettier/prettier": ["error", {}, { usePrettierrc: true }], // Include .prettierrc.js rules,
        "import/no-named-as-default": 0,
        "@typescript-eslint/explicit-function-return-type": 0,
        "@typescript-eslint/ban-ts-comment": [
            "error",
            {
                // use instead of @ts-ignore and provide explanation
                "ts-expect-error": "allow-with-description",
                minimumDescriptionLength: 2,
            },
        ],
        "@typescript-eslint/no-unused-vars": [
            2,
            {
                argsIgnorePattern: "^_",
            },
        ],
        "@typescript-eslint/explicit-module-boundary-types": 0, // export modules must have explicit return types
        "no-console": 0,
        "node/no-unpublished-import": 0,
        // ------ Remove / Revert when possible -----------
    },
    settings: {
        node: {
            resolvePaths: ["."],
            tryExtensions: [".ts"],
        },
        "import/resolver": {
            node: {
                moduleDirectory: ["."],
                extensions: [".ts"],
            },
        },
    },
};

我更喜欢对 typescript 和 javascript 使用 export。我使用的是esm库,但它不适用于我的打字稿设置。如何在 nodejs 上对 javascript 和 typescript 文件都使用 export 或在不禁用 eslint 的 no-undef 的情况下将 module.exports 用于 javascript 和 export 用于 typescript?

标签: javascriptnode.jstypescripteslint

解决方案


So basically now, we want to allow JS in TS. The following option helped me while I was facing similar issue.

{
    "compilerOptions": {
        ...
        "allowJs": true,
        ...
},

Reference : https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#writing-a-configuration-file


推荐阅读