简体   繁体   English

出口时需要声明或声明

[英]Declaration or statement expected when exporting

I'm exporting all my reducers from this index.js like this:我正在从这个index.js中导出我所有的减速器,如下所示:

export checking from 'reducers/Checking';
export saving from 'reducers/Saving';
export overdraft from 'reducers/Overdraft';
..........

I started getting the following error on every line above ^, when I try to integrate Typescript into my project:当我尝试将 Typescript 集成到我的项目中时,我开始在 ^ 上方的每一行上收到以下错误:

 Declaration or statement expected

I have stage-0 in my presets to support that kind of syntax, but still getting that error.我的预设中有stage-0来支持这种语法,但仍然出现该错误。

tsconfig.json tsconfig.json

Could anybody direct me to how towards how get rid of that error?有人可以指导我如何摆脱该错误吗?

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "es2015",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}
export * from 'reducers/Checking';
export * from 'reducers/Saving';
export * from 'reducers/Overdraft';

You have to try this.你必须试试这个。

The problem is that the form of export you're using doesn't exist.问题是您使用的导出形式不存在。 You can find a list of the forms in the spec here , but there isn't an export x from "y" form.您可以在此处的规范中找到 forms 的列表,但没有export x from "y"

It looks to me like you're trying to re-export the default export of each of those modules as a named export.在我看来,您正在尝试将每个模块的默认导出重新导出为命名导出。 That is, you have export default... in reducers/Checking.ts (for example) and want to re-export that from index.ts as checking .也就是说,您在reducers/Checking.ts中有export default... (例如),并希望从index.ts将其重新导出为checking If so, you have to use the named form and the identifier default explicitly:如果是这样,您必须明确使用命名形式和标识符default

// Re-exports the default export of each module
export { default as checking } from 'reducers/Checking';
export { default as saving } from 'reducers/Saving';
export { default as overdraft } from 'reducers/Overdraft';

But if you have named exports like export const checking... in reducers/Checking.ts (for example) and you're trying to re-export all of its named exports from index.ts , you'd do it like this:但是,如果您在reducers/Checking.ts (例如)中命名了export const checking...之类的导出,并且您尝试从index.ts重新导出其所有命名的导出,您可以这样做:

// Re-exports all named exports from each module:
export * from 'reducers/Checking';
export * from 'reducers/Saving';
export * from 'reducers/Overdraft';

Or if you were trying to export just a single named export , you'd do it like this:或者,如果您尝试仅导出一个名为 export ,您可以这样做:

// Re-exports a named export from each module:
export { checking } from 'reducers/Checking';
export { saving } from 'reducers/Saving';
export { overdraft } from 'reducers/Overdraft';

Finally, if you were trying to export a module namespace object for each of those modules (an object with a property for each of the module's exports), you'd do it like this:最后,如果您尝试为每个模块导出一个模块命名空间 object (一个 object,每个模块的导出都有一个属性),您可以这样做:

// Re-exports a named export from each module:
export * as checking from 'reducers/Checking';
export * as saving from 'reducers/Saving';
export * as overdraft from 'reducers/Overdraft';

All of the above assume reducers is in node_modules .以上所有假设reducersnode_modules中。 If it's your own directory instead, you need ./ at the beginning of those module specifiers ( from './reducers/Checking' instead of from 'reducers/Checking' ).如果它是您自己的目录,则需要./在这些模块说明符的开头( from './reducers/Checking'而不是from 'reducers/Checking' )。


Separately: To use JavaScript module syntax (often called ESM for E CMA S cript M odules), you need tell TypeScript that's what you're doing.另外:要使用 JavaScript 模块语法(对于E CMA S脚本模块通常称为 ESM),您需要告诉TypeScript这就是您正在做的事情。 There are a couple of ways to do that:有几种方法可以做到这一点:

  1. If you have "target": "ES6" or higher, TypeScript will assume you're using JavaScript modules (instead of CommonJS etc.)如果您有"target": "ES6"或更高版本,TypeScript 将假定您使用的是 JavaScript 模块(而不是 CommonJS 等)

  2. You can use "module": "ES2015" to explicitly opt into JavaScript modules.您可以使用"module": "ES2015"明确选择加入 JavaScript 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM