如何声明TypeScript导入*类型?

时间:2015-12-07 19:27:44

标签: types casting typescript type-conversion strong-typing

如何在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

2 个答案:

答案 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得到的)作为一个类,您将得到错误,这就是您所获得的。