在项目 .d.ts 定义文件中使用 @types 定义 [英] Use @types definitions in project .d.ts definition file

查看:79
本文介绍了在项目 .d.ts 定义文件中使用 @types 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目编写一个 .d.ts 文件来定义一些全局接口.但是,我在该定义文件中使用非全局库类型时遇到问题.尤其是我试图引用的 RxJs 对我不起作用.

I'm trying to write a .d.ts file for my project to define some global interfaces. 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.

我认为最有效的方法是使用三斜杠引用标签来导入 RxJs 的类型,但是这不起作用.

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.

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

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

推荐答案

我找到了解决方案:

import RxJs from 'rxjs';

declare global {

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

}

使用 declare global { } 我能够回到全局命名空间.Typescript 确实缺乏有关此有用指令的文档,仅在本页末尾略有记录:https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation.

Using declare global { } I was able to get back into the global namespace. 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.

这篇关于在项目 .d.ts 定义文件中使用 @types 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆