如何在TypeScript中重新导出单个lodash模块

时间:2019-01-11 13:06:39

标签: typescript

我正在尝试重新导出TypeScript应用程序中的单个lodash模块,如下所示:

npm i lodash
npm i @types/lodash
export {default as debounce} from 'lodash/debounce';

尝试使用tsc进行编译时,显示以下错误:

  

模块'“ ... / 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 个答案:

答案 0 :(得分:1)

看看lodash的去抖动source code,最后一行是:

export default debounce

这意味着函数debouncedebounce.js的默认导出。默认导出没有名称,因此在导入时可以将其重命名为任何有效的JavaScript名称。

要导入防抖动文件的默认导出,请不要使用花括号。然后将其导出到另一行:

import debounce = require('lodash/debounce');

export { debounce };

或从库中重新导出命名函数:

export { debounce } from 'lodash';