打字稿中的全局类型 [英] Global types in typescript

查看:38
本文介绍了打字稿中的全局类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在打字稿文件中创建一个定义全局可访问类型的文件?

Is there a way to make a file in your typescript file that defines globally accessible types?

我喜欢打字稿,但发现当我想要真正的类型安全时,我必须从整个系统中显式导入类型.比较烦.

I like typescript but find that when i want to be truly type safe I have to explicitly import types from all over the system. It's rather annoying.

推荐答案

是的,这是可能的.您可以在此处找到所有信息:https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html

Yes this is possible. You can find all information here: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html

重要的部分是:

declare global {
    /*~ Here, declare things that go in the global namespace, or augment
     *~ existing declarations in the global namespace
     */
    interface String {
        fancyFormat(opts: StringFormatOptions): string;
    }
}

这篇关于打字稿中的全局类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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