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.

Categories
TypeScript Answers

How to omit some items from an enum in TypeScript?

Sometimes, we want to omit some items from an enum in TypeScript.

In this article, we’ll look at how to omit some items from an enum in TypeScript.

How to omit some items from an enum in TypeScript?

To omit some items from an enum in TypeScript, we can use the Exclude type.

For instance, we write

enum ErrCode {
  Ok = 0,
  Error = 1,
  AccessDeny = 201,
  PostsNotFound = 202,
  TagNotFound = 203,
}

type ErrorErrcode = Exclude<ErrCode, ErrCode.Ok>;

to use Exclude to exclude the ErrCode.Ok value from the ErrCode type and return that as the type.

Then we assign that to the ErrorErrcode type alias.

Conclusion

To omit some items from an enum in TypeScript, we can use the Exclude type.

Categories
TypeScript Answers

How to define an array of strings in a TypeScript interface?

Sometimes, we want to define an array of strings in a TypeScript interface.

In this article, we’ll look at how to define an array of strings in a TypeScript interface.

How to define an array of strings in a TypeScript interface?

To define an array of strings in a TypeScript interface, we can set a property to a string variable.

For instance, we write

interface Addressable {
  address: string[];
}

to define the Addressable interface with the address property set to a string array with string[].

Conclusion

To define an array of strings in a TypeScript interface, we can set a property to a string variable.