简体   繁体   English

导出 typescript 类型定义

[英]Exporting typescript type definitions

In my project I'd like to have one file with all the typescript definitions like so:在我的项目中,我想要一个包含所有typescript定义的文件,如下所示:

type_defs.tsx : type_defs.tsx

interface T_oauth_obj {
    oauth_callback: string,
    oauth_consumer_key: string,
    oauth_nonce: string,
    oauth_signature_method: string,
    oauth_timestamp: string,
    oauth_version: number,
    oauth_signature: string,
}

interface IAuthRequests {
    getRequestToken: () => string
    Authorize(): void
    getAccessToken(): void  
}
...

The problem is that when i try to export the typedefs like so:问题是当我尝试像这样导出类型定义时:

export {IAuthRequests, T_oauth_obj}

I get the error:我得到错误:

Re-exporting a type when the '--isolatedModules'...

As I've found in the net there can be only the default export for a typedef file that means that i have to prepare one file per type definition which is absolutely an absurd!正如我在网上发现的那样,typedef 文件只能有默认导出,这意味着我必须为每个类型定义准备一个文件,这绝对是荒谬的!

In my opinion the constraint not allowing to have multiple, non-default exports for typedefs is totally nonsense and making the coding redundant and hard to re-use.在我看来,不允许对 typedef 进行多个非默认导出的约束完全是无稽之谈,并且使编码变得多余且难以重用。 Having one file with all the typedefs can help having more tidy and debug-able code.拥有一个包含所有 typedef 的文件有助于拥有更整洁和可调试的代码。

PS附言

Is there a way to achieve the goal I'm talking about?有没有办法实现我所说的目标?

Try like this:试试这样:

export interface T_oauth_obj {
    oauth_callback: string,
    oauth_consumer_key: string,
    oauth_nonce: string,
    oauth_signature_method: string,
    oauth_timestamp: string,
    oauth_version: number,
    oauth_signature: string,
}

export interface IAuthRequests {
    getRequestToken: () => string
    Authorize(): void
    getAccessToken(): void  
}

and then import:然后导入:

import { T_oauth_obj, IAuthRequests } from "your.file"

Your exported types can either be named, or each file can have one default type export.您的导出类型可以命名,或者每个文件可以有一个默认类型导出。 The export keyword simply does not allow for exporting multiple values. export关键字根本不允许导出多个值。

So, since it only works for one value or type at a time, this won't work because {IAuthRequests, T_oauth_obj} is not a valid single type.因此,由于它一次仅适用于一个值或一个类型,因此这将不起作用,因为{IAuthRequests, T_oauth_obj}不是有效的单一类型。

You could do a default export of a whole type, but this is probably not what you want:您可以执行整个类型的默认导出,但这可能不是您想要的:

export default { req: IAuthRequests, oauth: T_oauth_obj }

Or you could do:或者你可以这样做:

export interface IAuthRequests { ... }
export interface T_oauth_obj { ... }

If you want one export keyword to export multiple named types, then I have no other help than to say it just doesn't do that .如果您希望一个export关键字导出多个命名类型,那么我没有其他帮助,只能说它只是不这样做 To answer why you'll probably have to ask the typescript maintainers.要回答为什么您可能不得不询问 typescript 维护人员。 The answer is likely to be a deliberate design choice to more closely match how export works is plain JS.答案可能是经过深思熟虑的设计选择,以更接近地匹配export的工作方式是纯 JS。

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

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