[英]How to refer typings in typescript definition?
我正在写一个打字稿定义,我的目录是这样的:
src
task.ts
typings
task.d.ts
如果我这样写打字:
declare namespace task {
export interface TaskInfo {
line: string;
}
}
它工作正常,但现在我想引用其他类型的输入,例如vscode
,如下所示:
declare namespace task {
export interface TaskInfo {
line: string;
doc: vscode.TextDocument; // now it cannot find the module vscode
}
}
现在我必须导入vscode
,但是当我将其更改为:
import * as vscode from "vscode"; // import here
declare namespace task {
export interface TaskInfo {
line: string;
doc: vscode.TextDocument;
}
}
我收到这样的错误:
error TS2503: Cannot find namespace 'task'
如果我将其更改为:
declare namespace task {
import * as vscode from "vscode"; // import here
export interface TaskInfo {
line: string;
doc: vscode.TextDocument;
}
}
这次我得到这样的错误:
Import declarations in a namespace cannot reference a module.
那么,我应该怎样写我的打字?
文件中有import
或export
行后,该文件即成为模块。 这意味着该文件中定义的所有内容都仅作用于该文件。 如果要从另一个文件中使用它,则需要将其导入。
当您的文件没有任何import
或export
,该文件中的变量(或类型声明)将位于全局范围内。
所以这 -
declare namespace task {
export interface TaskInfo {
line: string;
doc: vscode.TextDocument; // now it cannot find the module vscode
}
}
声明一个名为task
的名称空间,该名称空间在全局范围内可用,并且可以从任何文件访问。 (这可能很糟糕!名称冲突等等)
一旦您import * as vscode from "vscode";
添加import * as vscode from "vscode";
该文件是一个模块,现在需要从您要使用的任何文件中导出和导入task
。 像这样:
import * as vscode from "vscode"; // import here
export declare namespace task {
export interface TaskInfo {
line: string;
doc: vscode.TextDocument;
}
}
/** in another file **/
import {task} from '../typings/task.d.ts';
...
这是选项1。(我认为更好)
我们还有另一个选择。 由于在javascript世界中,模块仍将内容放置在全局范围内并不少见,因此typescript确实允许这样做(但仅用于类型声明!您将无法在模块内实际在全局范围内创建值)
import * as vscode from "vscode"; // import here
declare global {
namespace task {
export interface TaskInfo {
line: string;
doc: vscode.TextDocument;
}
}
}
您将可以从任何其他文件访问task.TaskInfo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.