Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global import in Typescript?

I basically want to replace the Promise definition in Typescript (v2.0.10) with Bluebird. I read a lot about this but came out confused -- is it possible or not?

I really don't want to have to do this at the top of every TS file:

import * as Promise from "bluebird";

I tried to do this in my _stubs.d.ts to no avail:

import * as Bluebird from "bluebird";
declare var Promise: typeof Bluebird;
like image 927
Josh M. Avatar asked Nov 18 '16 04:11

Josh M.


People also ask

How do I import a global variable in TypeScript?

To declare a global variable in TypeScript, create a . d. ts file and use declare global{} to extend the global object with typings for the necessary properties or methods.

What is declare global in TypeScript?

declare global is used inside a file that has import or export to declare things in the global scope. This is necessary in files that contain import or export since such files are considered modules, and anything declared in a module is in the module scope.

How do I import files into TypeScript?

To import a class from another file in TypeScript: Export the class from file A , e.g. export class Employee {} . Import the class in file B as import { Employee } from './another-file' . Use the class in file B .

Does TypeScript support import?

With TypeScript 3.8, you can import a type using the import statement, or using import type .


1 Answers

  1. npm install --save-dev @types/bluebird-global
  2. Edit your tsconfig.json to list bluebird-global in the types array:

    {
      "compilerOptions": {
        "types": [
          "bluebird-global"
        ],
        // the rest of the options
      }
    }
    

Edit:

Step 2 is not necessary if you don't use the compilerOptions.types in your tsconfig.json.

like image 130
d-ph Avatar answered Nov 09 '22 00:11

d-ph