【发布时间】:2018-01-16 02:09:37
【问题描述】:
我非常喜欢 TS 2.4 中新的动态模块支持,虽然我遇到了这个小问题:
使用 import().then(...) 时,会给出模块的类型。如果我需要存储承诺以备后用,我无法为 Promise 生成传达模块类型信息的类型参数。
我正在尝试在包装反应组件中进行动态加载(我应该一直使用 react-router v4),并且我想在构造函数中开始加载 - 并在首次安装组件时重新加载。这有一个“闪烁”问题,但除此之外它还有效:
import * as React from 'react'
export class DashboardDynamic extends React.Component<any, any> {
constructor(props) {
super(props)
// At this point typescript knows the type of the import, including the "Dashboard" export
this.loaderPromise = import(/* webpackChunkName: "dashboard" */ './Dashboard')
}
componentDidMount() {
// Promise<any> makes the {Dashboard} component an "any" type
this.loaderPromise.then(({Dashboard}) => {
this.myContent = <Dashboard {...this.props}/>
this.forceUpdate()
})
}
myContent: JSX.Element = <div />;
loaderPromise: Promise<any> = null // PROBLEM: "any", not module type.
render() {
return this.myContent
}
}
有谁知道如何键入 promise 以使其保持动态模块的正确类型?
【问题讨论】:
标签: typescript