0
votes

Slowly converting a nodejs express javascript app to typescript app. However, due to nodejs using require / module.exports and typescript using import / export, eslint is constantly causing me headaches by reporting is eslint(no-undef) for require / module.exports. I don't want to disable 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"],
            },
        },
    },
};

I prefer to use export for both typescript and javascript. I was using the esm library but it doesn't work with my typescript setup. How can I either use export for both javascript and typescript files on nodejs or use module.exports for javascript and export for typescript without disabling eslint's no-undef?

1

1 Answers

1
votes

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