Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio code 如何正确声明给定模块的typescript全局类型以进行ts检查?_Visual Studio Code_Typescript Typings - Fatal编程技术网

Visual studio code 如何正确声明给定模块的typescript全局类型以进行ts检查?

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

我使用定义文件创建了一个模块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文件,我有:

/// <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;
}