Categories
TypeScript Answers

How to extend TypeScript global object in Node.js?

To extend TypeScript global object in Node.js, we can add a type definition file that extends the Global interface in the NodeJS module.

For instance, we write

declare module NodeJS {
  interface Global {
    foo: any;
  }
}

in a .d.ts file in our Node TypeScript project to add the foo property into the Global interface.

Then we can use the variable without compiler errors by using global.foo.

Conclusion

To extend TypeScript global object in Node.js, we can add a type definition file that extends the Global interface in the NodeJS module.

Categories
TypeScript Answers

How to define a private property when implementing an interface in TypeScript?

Sometimes, we want to define a private property when implementing an interface in TypeScript.

In this article, we’ll look at how to define a private property when implementing an interface in TypeScript.

How to define a private property when implementing an interface in TypeScript?

To define a private property when implementing an interface in TypeScript, we can add private properties into the class that implements the interface.

For instance, we write

interface IModuleMenuItem {
  getName(): string;
}

class ModuleMenuItem implements IModuleMenuItem {
  private name;

  public getName() {
    return name;
  }

  protected setName(newName: string) {
    name = newName;
  }
}

to add the name private variable into the ModuleMenuItem class.

All interface properties must be public.

Conclusion

To define a private property when implementing an interface in TypeScript, we can add private properties into the class that implements the interface.

Categories
TypeScript Answers

How to disable TypeScript warnings in VS Code?

Sometimes, we want to disable TypeScript warnings in VS Code.

In this article, we’ll look at how to disable TypeScript warnings in VS Code.

How to disable TypeScript warnings in VS Code?

To disable TypeScript warnings in VS Code, we can set a few options in our VS Code settings to disable them.

For instance, we add

{
  //...
  "typescript.validate.enable": false,
  "javascript.validate.enable": false
  //...
}

in our settings.json file to disable TypeScript and JavaScript warnings by setting typescript.validate.enable and javascript.validate.enable to false respectively.

Conclusion

To disable TypeScript warnings in VS Code, we can set a few options in our VS Code settings to disable them.

Categories
TypeScript Answers

How to use Promise.all() with TypeScript?

Sometimes, we want to use Promise.all() with TypeScript.

In this article, we’ll look at how to use Promise.all() with TypeScript.

How to use Promise.all() with TypeScript?

To use Promise.all() with TypeScript, we can use it as we do with JavaScript.

For instance, we write

const [foo, bar] = await Promise.all([fooPromise, barPromise]);

in an async function.

We call Promise.all to return a promise with an array of the resolved values from the array of promises we passed into Promise.all.

The TypeScript compiler will infer the type automatically and make sure they’re consistent between both objects so we don’t have to specify them ourselves.

Conclusion

To use Promise.all() with TypeScript, we can use it as we do with JavaScript.

Categories
TypeScript Answers

How to set an import path alias with TypeScript?

Sometimes, we want to set an import path alias with TypeScript.

In this article, we’ll look at how to set an import path alias with TypeScript.

How to set an import path alias with TypeScript?

To set an import path alias with TypeScript, we can add the alias to the paths option array in tsconfig.json.

For instance, we write

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "angular2/*": ["../path/to/angular2/*"],
      "local/*": ["../path/to/local/modules/*"]
    }
  }
}

to add the angular/2 alias which is set to ["../path/to/angular2/*"].

We also add the local/* alias which is set to ["../path/to/local/modules/*"].

Then in our project, we can import members from the listed paths with

import { bootstrap } from "angular2/bootstrap";
import { module } from "local/module";

Conclusion

To set an import path alias with TypeScript, we can add the alias to the paths option array in tsconfig.json.