如何在typeScript中将import *的类型显式声明为...
我有这段代码:
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
答案 0 :(得分:1)
陷入同样的问题。基本上想要
import * as Util from './Util'
然后使用Util作为参数。这可以通过使用typeof Util
function thing(localUtil: typeof Util) {...}
thing(Util);
答案 1 :(得分:0)
从&#39; ./ interfaces / InterfaceA&#39;
导入*作为UnauthorizedInterface
我想你可能意味着做import {UnauthorizedInterface} from './interfaces/InterfaceA'
。当然,如果您使用模块(这是您使用* as
得到的)作为一个类,您将得到错误,这就是您所获得的。