简体   繁体   English

如何声明TypeScript import *类型?

[英]How to declare TypeScript import * type?

How can I explicitly declare type at import * as ... in TypeScript? 如何在TypeScript中将import *的类型显式声明为...?

I'm having this code: 我有此代码:

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}
...

and I'm getting 而我得到

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"'.

though it's only exporting entities extending React.ComponentClass 虽然只是导出扩展了React.ComponentClass的实体

import * as UnauthorizedInterface from './interfaces/InterfaceA' 从'./interfaces/InterfaceA'导入*作为UnauthorizedInterface

I think you probably mean to do import {UnauthorizedInterface} from './interfaces/InterfaceA' . 我认为您可能打算import {UnauthorizedInterface} from './interfaces/InterfaceA' Of course if you use the module (which is what you get with * as ) as a class you would get the error which is what you are getting. 当然,如果将模块 (使用* as获得的模块 )用作类,则会得到错误,即得到的错误。

Ran into the same issue. 遇到同样的问题。 Basically wanted to 基本上想

import * as Util from './Util'

And then use Util as an argument. 然后使用Util作为参数。 This works by using typeof Util 这通过使用typeof Util起作用

function thing(localUtil: typeof Util) {...}

thing(Util);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM