如何在 TypeScript 中显式声明 import * as ... 中的类型?
我有这段代码:
import * as UnauthorizedInterface from './interfaces/InterfaceA'
import * as AuthorizedInterface from './interfaces/InterfaceB'
export class App extends React.Component<{children?: React.ReactChildren, authorized?: Boolean}, {}> {
public render() {
return <Router>
<Route component={Interface}>
<Route path="/application" components={InterfaceA}
...
我得到了
ERROR in ./src/components/App.tsx
(16,24): error TS2322: Type 'typeof "~/src/interfaces/InterfaceA"'' is not assignable to type '{ [key: string]: string | ComponentClass<any> | StatelessComponent<any>; }'.
Index signature is missing in type 'typeof "~/src/interfaces/InterfaceA"'.
尽管它只是导出扩展 React.ComponentClass 的实体
最佳答案
import * as UnauthorizedInterface from './interfaces/InterfaceA'
我想你可能是想做 import {UnauthorizedInterface} from './interfaces/InterfaceA'
。当然,如果您使用模块(这是您通过* as
获得的)作为一个类,您将得到您所得到的错误。
https://stackoverflow.com/questions/34141572/