打字稿异步/等待转换为产生错误

时间:2018-08-03 06:12:24

标签: javascript reactjs typescript ecmascript-6 async-await

我正在尝试进行异步/等待操作

private onSubmit = async (event: React.FormEvent<HTMLFormElement>): Promise<void> => {
    event.preventDefault()
    this.props.onSubmit(this.state.takerAddress)
    try {
      const balance = await someAsyncFn()
      console.log('BALANCE:', balance)
    } catch (err) {
      console.log(err)
    }

我收到此错误:

You may need an appropriate loader to handle this file type.
         this.props.onSubmit(this.state.takerAddress);
         try {
             const balance = yield someAsyncFn(), console, log; // line 30
             ('BALANCE:', balance);
         }
 @ ./src/components/Search.tsx 4:15-32
 @ ./src/components/App.tsx
 @ ./src/index.tsx
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index

ERROR in [at-loader] ./src/components/Form.tsx:29:73
    TS1005: ',' expected.

ERROR in [at-loader] ./src/components/Form.tsx:30:14
    TS1005: ',' expected.

ERROR in [at-loader] ./src/components/Form.tsx:30:18
    TS1005: ',' expected.

ERROR in [at-loader] ./src/components/Form.tsx:30:19
    TS2695: Left side of comma operator is unused and has no side effects.

这是我的tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": false,
    "sourceMap": true,
    "declaration": true,
    "strictNullChecks": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react",
    "typeRoots": ["node_modules/@types"]
  },
  "include": [
    "./src/**/*"
  ]
}

如您所见,它在这里编译很奇怪,我不确定为什么。这是因为我在tsconfig中缺少什么吗?

1 个答案:

答案 0 :(得分:-1)

tsconfig的目标是es6。关键字async/await稍后介绍。因此,在底层,打字稿编译器使用基于生成器的帮助器来模仿此行为。 如果可以,请尝试突破目标选项。