Categories
TypeScript Answers

How to declare a type globally in a project with TypeScript?

Sometimes, we want to declare a type globally in a project with TypeScript.

In this article, we’ll look at how to declare a type globally in a project with TypeScript.

How to declare a type globally in a project with TypeScript?

To declare a type globally in a project with TypeScript, we can use the declare statement in a .d.ts file.

For instance, we add

declare module "my-module" {
  export declare class Something {
    public something: number;
  }
}

into a .d.ts file into our TypeScript project folder to declare the types for the my-module module.

We declare that it has class Something and it has the something number instance property.

Conclusion

To declare a type globally in a project with TypeScript, we can use the declare statement in a .d.ts file.

Categories
TypeScript Answers

How to get the type of a property of a TypeScript class using keyof operator?

Sometimes, we want to get the type of a property of a TypeScript class using keyof operator.

In this article, we’ll look at how to get the type of a property of a TypeScript class using keyof operator.

How to get the type of a property of a TypeScript class using keyof operator?

To get the type of a property of a TypeScript class using keyof operator, we can use lookup types.

For instance, we write

type BarType = FooType["bar"];

to get the type of the bar property of an object of FooType with FooType["bar"].

And then we assign the returned type to BarType.

Conclusion

To get the type of a property of a TypeScript class using keyof operator, we can use lookup types.

Categories
TypeScript Answers

How to add return type for a function that returns another function in TypeScript?

To add return type for a function that returns another function in TypeScript, we can define a return type that returns a function like any other type.

For instance, we write

export const add: (a: number) => (b: number) => number = (a) => (b) => a + b;

to define the add function that takes a number argument and returns a function that takes another number argument and that returns a number.

And then we assign it a function that matches the signature and return type.

We can also substitute number for a generic type.

To do this, we write

const add: <A extends number, B extends number>(a: A) => (b: B) => number =
  (a) => (b) =>
    a + b;
``

And we substitute `number` for types `A` and `B` in the function signatures of the type definition.
Categories
TypeScript Answers

How to use Moment.js with TypeScript?

Sometimes, we want to use Moment.js with TypeScript.

In this article, we’ll look at how to use Moment.js with TypeScript.

How to use Moment.js with TypeScript?

To use Moment.js with TypeScript, we can use the types provided by moment.

For instance, we write

import * as moment from "moment";

class DateClass {
  constructor() {}

  public getDate(): moment.Moment {
    return moment();
  }
}

to import the whole moment module with

import * as moment from "moment";

Then in the getDate method, we set the return type of it to moment.Moment, which is the type for objects returned by moment.

Therefore, returning moment() matches the moment.Moment and the TypeScript compiler will accept this code.

Conclusion

To use Moment.js with TypeScript, we can use the types provided by moment.

Categories
TypeScript Answers

How to use promise generic types with TypeScript?

Sometimes, we want to use promise generic types with TypeScript.

In this article, we’ll look at how to use promise generic types with TypeScript.

How to use promise generic types with TypeScript?

To use promise generic types with TypeScript, we can use the Promise generic type.

For instance, we write

const test = (arg: string): Promise<number> => {
  return new Promise<number>((resolve, reject) => {
    if (arg === "a") {
      resolve(1);
    } else {
      reject("1");
    }
  });
};

to create the test function which returns a promise.

To add the return type for the return value, we set the return type to Promise<number>.

This means the promise return would resolve to a number.

Therefore, call resolve with a number in the Promise constructor callback would match the required return type.

Conclusion

To use promise generic types with TypeScript, we can use the Promise generic type.