简体   繁体   English

在项目.d.ts定义文件中使用@types定义

[英]Use @types definitions in project .d.ts definition file

I'm trying to write a .d.ts file for my project to define some global interfaces. 我正在尝试为我的项目编写一个.d.ts文件,以定义一些全局接口。 However I have a problem using non-global library types inside that definition file. 但是我在该定义文件中使用非全局库类型时遇到问题。 Especially RxJs which I'm trying to reference does not work for me. 特别是我要参考的RxJ对我不起作用。

The thing I thought would work the best was using a triple-slash reference tag to import the types for RxJs, however this didn't work. 我认为最有效的方法是使用三斜杠引用标记导入RxJ的类型,但这没有用。

/// <reference types="rxjs" />

interface IUserService {
    user$: rxjs.Observable<IUser>;
}

I found the solution to what I wanted to do: 我找到了想做的解决方案:

import RxJs from 'rxjs';

declare global {

    interface IUserService {
        user$: RxJs.Observable<IUser>;
    }

}

Using declare global { } I was able to get back into the global namespace. 使用declare global { }我可以回到全局名称空间。 Typescript is really lacking documentation on this useful directive which is only documented slightly at the end of this page: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation . Typescript确实缺少有关此有用指令的文档,该文档仅在此页末尾进行了少量记录: https : //www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation

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

相关问题 使用 .d.ts 文件中的类型定义而不导入 - Use type definitions from .d.ts file without importing 如何使用在 adts 文件中定义的类型的模块? - How to use a module with types defined in a .d.ts file? 在定义文件 (*d.ts) 中导入类 - Import class in definition file (*d.ts) 如果库的实现没有与TS项目集成,如何使用d.ts文件的导出的const类型? - How to consume a d.ts file's exported const types if the library's implementation is not integrated with the TS project? Typescript:改变一些第三方类型d.ts的定义 - Typescript: Changing the definition of some third party types d.ts 如何使用 d.ts 文件中的类型定义作为 js 文件中的 doctype? - How to use type definition from d.ts file as doctype in js file? 正确使用 Typescript .d.ts 文件 - Proper use of a Typescript .d.ts file VSCODE 跳转到JS文件定义(不是.d.ts文件) - VSCODE Jump to the JS file definition (not the .d.ts file) 有没有一种方法可以在TypeScript上使用不带d.ts定义的npm软件包 - Is there a way to use a npm package without d.ts definition on TypeScript 引入类型以在带有声明模块的.d.ts文件中使用,而不使其成为模块文件 - Pulling in types for use in a .d.ts file with declare module without making it a module file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM