简体   繁体   English

你如何引用 lib.d.ts 类型

[英]How do you refer to lib.d.ts types

Is there a way to refer to lib.d.ts types in Typescript?有没有办法在 Typescript 中引用 lib.d.ts 类型? I'm making a class called File that uses the lib.d.ts File type.我正在创建一个名为 File 的类,它使用 lib.d.ts 文件类型。

File.ts文件.ts

module SomeModule {
   export class File{
      ...
      public foo(file:lib.ts.File) {
          // do stuff
      }
      ...
      public bar(file:SomeModule.File){
          // do some more stuff
      }
   }
}

A similar question was asked over a year ago on codeplex but I couldn't find out if anything has changed since then or if a workaround (that doesn't involve renaming SomeModule.File ) existed.一年多以前在codeplex上有人问过一个类似的问题,但我不知道从那以后是否有任何变化,或者是否存在解决方法(不涉及重命名SomeModule.File )。

Since TypeScript 1.4 you may also use type aliases:从 TypeScript 1.4 开始,您还可以使用类型别名:

type g_Event = Event;
module Lib {
    export interface Event extends g_Event {
        target: Element;
    }
    export function create(e: g_Event): Event{
        //...
    }
}

There are workarounds.有解决方法。 You can use declared (no JS generated!) global variable to capture the type information and use it locally :您可以使用声明的(没有 JS 生成!)全局变量来捕获类型信息并在本地使用:

declare var FileTypeCapture:File;

module SomeModule {
   export class File{
      public foo(file:typeof FileTypeCapture) {
          // do stuff

      }
      public bar(file:SomeModule.File){
          // do some more stuff
      }
   }
}

Since TypeScript 3.4 you can use globalThis to access these types:从 TypeScript 3.4 开始,您可以使用 globalThis 来访问这些类型:

module SomeModule {
   export class File{
      ...
      public foo(file: globalThis.File) {
          // do stuff
      }
      ...
      public bar(file: File){
          // do some more stuff
      }
   }
}

See https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#type-checking-for-globalthis请参阅https://devblogs.microsoft.com/typescript/annoucing-typescript-3-4/#type-checking-for-globalthis

Currently this isn't possible (TypeScript 1.3).目前这是不可能的(TypeScript 1.3)。

There is a issue on GitHub with a suggestion for this behaviour github which you might want to read! GitHub 上有一个问题,您可能需要阅读关于此行为github的建议!

In that issue thread there are two suggestions, one is global::File and the other one is global.File .在那个问题线程中有两个建议,一个是global::File ,另一个是global.File

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

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