Categories
TypeScript Answers

How to make a variable final in TypeScript?

Sometimes, we want to make a variable final in TypeScript.

In this article, we’ll look at how to make a variable final in TypeScript.

How to make a variable final in TypeScript?

To make a variable final in TypeScript, we can use the readonly keyword.

For instance, we write

class Foo {
  readonly bar = 1; 
  readonly baz: string;
  constructor() {
    this.baz = "hello"; 
  }
}

to make the bar and baz variables readonly.

This means we can’t assign an new value to the variables after they’ve been assigned a value.

Conclusion

To make a variable final in TypeScript, we can use the readonly keyword.

Categories
TypeScript Answers

How to allow an optional parameter to be null in TypeScript?

Sometimes, we want to allow an optional parameter to be null in TypeScript.

In this article, we’ll look at how to allow an optional parameter to be null in TypeScript.

How to allow an optional parameter to be null in TypeScript?

To allow an optional parameter to be null in TypeScript, we can add the null type in a union with other types.

For instance, we write

const foo = (bar: string | null) => {
  console.info(bar);
};

foo("Hello World!"); 
foo(null);

to let us call foo with the bar parameter set to a string or null with the string | null union type.

Then we call foo as we do above without any errors.

Conclusion

To allow an optional parameter to be null in TypeScript, we can add the null type in a union with other types.

Categories
TypeScript Answers

How to await a list of promises in JavaScript or TypeScript?

Sometimes, we want to await a list of promises in JavaScript or TypeScript.

In this article, we’ll look at how to await a list of promises in JavaScript or TypeScript.

How to await a list of promises in JavaScript or TypeScript?

To await a list of promises in JavaScript or TypeScript, we call Promise.all with an array of promises.

For instance, we write

const bar = await Promise.all(promiseArray);

in an async function to call Promise.all with a promiseArray which is an array of promises.

Then we use await to return an array of the resolve value of all the promises and assign it to bar.

Conclusion

To await a list of promises in JavaScript or TypeScript, we call Promise.all with an array of promises.

Categories
TypeScript Answers

How to get type of array items with TypeScript?

Sometimes, we want to get type of array items with TypeScript.

In this article, we’ll look at how to get type of array items with TypeScript.

How to get type of array items with TypeScript?

To get type of array items with TypeScript, we can use lookup types.

For instance, we write

type Foo = Array<{ name: string; test: number }>;
type FooItem = Foo[0];

to get the type for array entries in the Foo type.

To do this, we just use Foo[0] to return the type of the items in with the Foo array type.

And we assign the returned type to FooItem.

Conclusion

To get type of array items with TypeScript, we can use lookup types.

Categories
TypeScript Answers

How to access TypeScript enum by ordinal?

Sometimes, we want to access TypeScript enum by ordinal.

In this article, we’ll look at how to access TypeScript enum by ordinal.

How to access TypeScript enum by ordinal?

To access TypeScript enum by ordinal, we can get the index of the enum entry with Object.keys and array indexOf.

For instance, we write

enum LookingForEnum {
  Romantic = "Romantic",
  Casual = "Casual",
  Friends = "Friends",
  Fun = "Fun",
}

const index: number = Object.keys(LookingForEnum).indexOf("Casual");

to call Object.keys with LookingForEnum to return the keys in the enum.

Then we call indexOf with the key we’re looking for as a string to return the index of the entry.

Therefore, index is 1.

Conclusion

To access TypeScript enum by ordinal, we can get the index of the enum entry with Object.keys and array indexOf.