如何在 TypeScript 中重新导出单个 lodash 模块
Posted
技术标签:
【中文标题】如何在 TypeScript 中重新导出单个 lodash 模块【英文标题】:How to re-export an individual lodash module in TypeScript 【发布时间】:2019-06-06 09:55:19 【问题描述】:我正在尝试在 TypeScript 应用程序中重新导出单个 lodash 模块,如下所示:
npm i lodash
npm i @types/lodash
export default as debounce from 'lodash/debounce';
尝试使用 tsc 编译时显示以下错误:
Module '".../node_modules/@types/lodash/debounce"' 解析为非模块实体,无法使用此构造导入。
我似乎无法理解这意味着什么?
tsconfig.json:
"compilerOptions":
"moduleResolution": "node",
"baseUrl": "./",
"paths":
,
"target": "es5",
"module": "es6",
"lib": [
"es2015",
"es2016",
"es2017",
"dom",
"scripthost"
],
"downlevelIteration": true,
"jsx": "react",
"allowJs": true,
"checkJs": false,
"sourceMap": true,
"noEmit": true,
"strict": true,
"noImplicitAny": false,
"strictNullChecks": true,
"strictFunctionTypes": false,
"strictPropertyInitialization": false,
"noImplicitThis": false,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": false,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
,
"include": [
"src"
],
"exclude": [
]
tslint.json:
"defaultSeverity": "error",
"extends": [
"tslint:recommended",
"tslint-react"
],
"linterOptions":
"exclude": [
"doc/**",
"interfaces.flow/**",
"node_modules/**",
"rollout/**",
"temp/**",
"vendor/**"
]
,
"rules":
"array-type": false,
"arrow-parens": false,
"ban-types": false,
"comment-format": false,
"forin": false,
"indent": [true, "tabs"],
"interface-name" : false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"jsx-no-multiline-js": false,
"max-classes-per-file": false,
"max-line-length": [true, 500],
"member-access": false,
"no-consecutive-blank-lines": false,
"no-console": false,
"no-empty": false,
"no-shadowed-variable": [
true,
"temporalDeadZone": false
],
"no-string-literal": false,
"member-ordering": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"object-literal-sort-keys": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"ordered-imports": false,
"prefer-for-of": false,
"quotemark": [true, "single", "jsx-double", "avoid-escape", "avoid-template"],
"semicolon": [true, "always"],
"space-before-function-paren":
"options":
"anonymous": "always",
"asyncArrow": "always",
"constructor": "never",
"method": "never",
"named": "never"
,
"trailing-comma": [
true,
"esSpecCompliant": true
],
"variable-name":
"options": ["ban-keywords", "check-format", "allow-pascal-case", "allow-leading-underscore"]
,
"rulesDirectory": []
【问题讨论】:
【参考方案1】:看lodash的去抖source code,最后一行是:
export default debounce
这意味着函数debounce
是debounce.js
的默认导出默认导出没有名称,因此在导入时可以将其重命名为任何有效的javascript名称。
要导入 debounce 文件的默认导出,请勿使用花括号。然后将其导出到单独的行:
import debounce = require('lodash/debounce');
export debounce ;
或从库中重新导出命名函数:
export debounce from 'lodash';
【讨论】:
使用export debounce from 'lodash/debounce';
会产生多个错误:Declaration or statement expected.
、Cannot find name 'debounce'.
、';' expected.
、Cannot find name 'from'.
和 ';' expected.
。使用 export debounce from 'lodash';
确实有效,但正如我所提到的,我只想使用单独的 lodash 模块。
另请注意,根据 MDN developer.mozilla.org/en-US/docs/web/javascript/reference/…,export default as...
实际上是允许的
我已经更新了我的答案。如果您正在使用它们,您可能希望将您的 tsconfig.json
和 tslint.json
添加到原始问题中。你是什么意思“单独的 lodash 模块?你知道这个npmjs.com/package/lodash.debounce如果你还需要帮助,请告诉我
我添加了tsconfig.json
和tslint.json
。对于单个模块,我的意思是导入lodash/debounce
而不是lodash
。除了单独导入和导出之外,我仍然不知道如何重新导出模块。我与 babel export default as debounce from 'lodash/debounce';
一起使用的语法没有按预期运行,并给我错误消息 Module '".../node_modules/@types/lodash/debounce"' resolves to a non-module entity and cannot be imported using this construct.
。根据错误消息,它似乎与类型有关,而不是与导入本身有关。以上是关于如何在 TypeScript 中重新导出单个 lodash 模块的主要内容,如果未能解决你的问题,请参考以下文章
如何在 typescript/javascript 导出中使用字符串模板来处理 graphql-codegen 架构