Visual studio code 如何正确声明给定模块的typescript全局类型以进行ts检查?
我使用定义文件创建了一个模块Xpto的图像:Visual studio code 如何正确声明给定模块的typescript全局类型以进行ts检查?,visual-studio-code,typescript-typings,Visual Studio Code,Typescript Typings,我使用定义文件创建了一个模块Xpto的图像: // node_modules/@types/xpto.d.ts export interface Vertx { createHttpServer(handler: () => void) : void; } 并声明一个助手来跟踪我的全局对象: // runtime.d.ts import {Xpto} from 'xpto'; declare const xpto: Xpto; 最后,关于一个JS文件,我有: /// <re
// node_modules/@types/xpto.d.ts
export interface Vertx {
createHttpServer(handler: () => void) : void;
}
并声明一个助手来跟踪我的全局对象:
// runtime.d.ts
import {Xpto} from 'xpto';
declare const xpto: Xpto;
最后,关于一个JS文件,我有:
/// <reference path="runtime.d.ts" />
// @ts-check
xpto.createHttpServer(function (req) { ... });
但是,由于没有真正的运行时模块,这将生成中断的代码。这只是我声明哪些变量在全局范围内可用的一种方法。我认为问题在于
导入
和导出
将d.ts
文件转换为模块声明。这就是为什么全局版本不起作用,而带有import
的版本起作用
尝试使用declare global
将xpto
显式声明为全局:
import {Xpto} from 'xpto';
declare global {
declare const xpto: Xpto;
}
import {Xpto} from 'xpto';
declare global {
declare const xpto: Xpto;
}