延迟加载时没有任何构造或调用签名

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

标签: reactjs typescript lazy-loading

我对这个感到有些困惑...

我正在懒惰地加载整个模块,但是由于某种原因,TypeScript似乎对此有问题,我不确定为什么。

我有时会在组件中运行

import('./foo/foo').then(module => this.setState({ Foo: module.foo() }));

然后在我的render()函数中,我得到一些提示

{ this.state.Foo != null ? <this.state.Foo /> : null }

./foo/foo文件确实导出了一个函数,如下所示:

export function foo() {
  return class Foo extends React.Component {
    // ... stuff
  }
}

但是,TypeScript经常说:

TS2604: JSX element type 'Foo' does not have any construct or call signatures.

State被定义为

type State = { Foo: React.ComponentClass<any, any> | null };

我想念什么?

哦,也许派上用场了,我正在使用TypeScript 3.0.1,这是我的配置:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "strict": true,
    "strictFunctionTypes": true,
    "lib": [
      "es5",
      "es2015",
      "dom",
      "scripthost",
      "es2016.array.include",
      "es2017.object",
      "dom.iterable"
    ],
    "outDir": "dist/out-tsc",
    "jsx": "react",
    "allowJs": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "alwaysStrict": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noStrictGenericChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "suppressImplicitAnyIndexErrors": true,
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "target": "es5",
    "allowSyntheticDefaultImports": false,
    "typeRoots": ["node_modules/@types", "src/@types"]
  },
  "compileOnSave": true
}

0 个答案:

没有答案