简体   繁体   English

如何在打字稿定义中引用打字?

[英]How to refer typings in typescript definition?

I am writing a typescript definition, my directory is like this: 我正在写一个打字稿定义,我的目录是这样的:

src
    task.ts
typings
    task.d.ts

if I write typing like this: 如果我这样写打字:

declare namespace task {
    export interface TaskInfo {
        line: string;
    }
}

it works fine, but now I want to refer typing in other typings, for example vscode , like this: 它工作正常,但现在我想引用其他类型的输入,例如vscode ,如下所示:

declare namespace task {
    export interface TaskInfo {
        line: string;
        doc: vscode.TextDocument; // now it cannot find the module vscode
    }
}

now I have to import vscode , but when I change it to: 现在我必须导入vscode ,但是当我将其更改为:

import * as vscode from "vscode"; // import here
declare namespace task {
    export interface TaskInfo {
        line: string;
        doc: vscode.TextDocument;
    }
}

I get error like this: 我收到这样的错误:

error TS2503: Cannot find namespace 'task'

if I change it to this: 如果我将其更改为:

declare namespace task {
    import * as vscode from "vscode";  // import here
    export interface TaskInfo {
        line: string;
        doc: vscode.TextDocument;
    }
}

this time I get error like this: 这次我得到这样的错误:

Import declarations in a namespace cannot reference a module.

So, how should I write my typings? 那么,我应该怎样写我的打字?

Once you have an import or export line in your file, that file becomes a module. 文件中有importexport行后,该文件即成为模块。 This means that everything defined inside that file is scoped only to that file. 这意味着该文件中定义的所有内容都仅作用于该文件。 If you want to use it from another file, you'll need to import it. 如果要从另一个文件中使用它,则需要将其导入。

When your file doesn't have any import or export , variables (or type declarations) in that file sit at the global scope. 当您的文件没有任何importexport ,该文件中的变量(或类型声明)将位于全局范围内。

so this - 所以这 -

declare namespace task {
    export interface TaskInfo {
        line: string;
        doc: vscode.TextDocument; // now it cannot find the module vscode
    }
}

declares a namespace called task that is available at the global scope, and accessible from any file. 声明一个名为task的名称空间,该名称空间在全局范围内可用,并且可以从任何文件访问。 (Which can be bad! name collisions and all) (这可能很糟糕!名称冲突等等)

once you add import * as vscode from "vscode"; 一旦您import * as vscode from "vscode";添加import * as vscode from "vscode"; that file is a module, and now task needs to be exported, and imported from any file you want to use. 该文件是一个模块,现在需要从您要使用的任何文件中导出和导入task Something like this: 像这样:

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';
...

This is option 1. (Which I think is better) 这是选项1。(我认为更好)

We have another option. 我们还有另一个选择。 Since in the javascript world it's not uncommon for modules to still put stuff on the global scope, typescript does allow this (but only for type declarations! you won't be able to actually create values on the global scope from within a module) 由于在javascript世界中,模块仍将内容放置在全局范围内并不少见,因此typescript确实允许这样做(但仅用于类型声明!您将无法在模块内实际在全局范围内创建值)

import * as vscode from "vscode"; // import here
declare global {
  namespace task {
    export interface TaskInfo {
        line: string;
        doc: vscode.TextDocument;
    }
  }
}

you will be able to access task.TaskInfo from any other file. 您将可以从任何其他文件访问task.TaskInfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM