简体   繁体   中英

How do you refer to lib.d.ts types

Is there a way to refer to lib.d.ts types in Typescript? I'm making a class called File that uses the lib.d.ts File type.

File.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.

Since TypeScript 1.4 you may also use type aliases:

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 :

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:

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

Currently this isn't possible (TypeScript 1.3).

There is a issue on GitHub with a suggestion for this behaviour github which you might want to read!

In that issue thread there are two suggestions, one is global::File and the other one is global.File .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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