javascript - 需要 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?
解决方案
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
推荐阅读
- unit-testing - 数据驱动单元测试中的执行控制。(MSUnitTest)
- javascript - 初始化后如何更新 jQuery 无选择器插件上的选项?
- reactjs - 有没有办法在组件安装后以特定间隔更新 react-bootstrap-daterangepicker 的日期范围?
- elixir - 在 Elixir 中切换真假
- android - 获取firebase服务器时间戳的简单日期?如何只获取日期而不是时间
- python - 我的雅虎金融报价数据的 Python BeautifulSoup 刮板坏了。上周有什么变化吗?
- windows - 在 Windows 上从英特尔 SGX enclave 代码访问 uRSP 和 uRBP
- javascript - Angular - 在页面内容中查找,突出显示并跳转到页面中的位置(如在 vscode 中)
- html - Bootstrap的div中右对齐第一列和左对齐第二列
- amazon-web-services - Docker CannotCreateContainerError: Thin Pool 有 0 个空闲数据块