thed0ctor
thed0ctor

Reputation: 1406

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.

Upvotes: 3

Views: 816

Answers (4)

Aaron
Aaron

Reputation: 383

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

Upvotes: 2

Alexander Shutau
Alexander Shutau

Reputation: 2830

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{
        //...
    }
}

Upvotes: 3

basarat
basarat

Reputation: 276383

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

Upvotes: 1

Dick van den Brink
Dick van den Brink

Reputation: 14587

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.

Upvotes: 0

Related Questions