【问题标题】:Need both javascript and typescript files to work on nodejs express app需要 javascript 和 typescript 文件才能在 nodejs express 应用程序上工作
【发布时间】:2020-07-30 04:51:54
【问题描述】:

慢慢地将 nodejs express javascript 应用程序转换为 typescript 应用程序。然而,由于 nodejs 使用 require / module.exports 和 typescript 使用 import / export,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?

【问题讨论】:

    标签: javascript node.js typescript eslint


    【解决方案1】:

    所以基本上现在,我们希望在 TS 中允许 JS。当我遇到类似问题时,以下选项对我有所帮助。

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多