Categories
TypeScript Answers

How to import an enum with TypeScript?

Sometimes, we want to import an enum with TypeScript.

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

How to import an enum with TypeScript?

To import an enum with TypeScript, we need to export it before we can import it.

To export it, we write

export enum EntityStatus {
  New = 0,
  Active = 1,
  Archived = 2,
  Trashed = 3,
  Deleted = 4,
}

to export the EntityStatus enum with the export keyword.

Then we can import it with

import { EntityStatus } from "../../core/enums";

Conclusion

To import an enum with TypeScript, we need to export it before we can import it.

Categories
TypeScript Answers

How to programmatically enumerate an enum type with TypeScript?

Sometimes, we want to programmatically enumerate an enum type with TypeScript.

In this article, we’ll look at how to programmatically enumerate an enum type with TypeScript.

How to programmatically enumerate an enum type with TypeScript?

To programmatically enumerate an enum type with TypeScript, we can use the Object.keys to get the keys and Object.values to get the values.

For instance, we write

const Color = {
  "RED": "Red",
  "ORANGE": "Orange",
  "YELLOW": "Yellow",
  "GREEN": "Green",
  "BLUE": "Blue",
  "INDIGO": "Indigo",
  "VIOLET": "Violet"
}

const colorKeys = Object.keys(Color) as (keyof typeof Color)[];
const colorValues = Object.values(Color);

to call Object.keys with the Color enum to return the keys on the left side in an array.

Then we call Object.values with Color to return an array with the values on the right side in an array.

Conclusion

To programmatically enumerate an enum type with TypeScript, we can use the Object.keys to get the keys and Object.values to get the values.

Categories
TypeScript Answers

How to use enums inside a class with TypeScript?

Sometimes, we want to use enums inside a class with TypeScript

In this article, we’ll look at how to use enums inside a class with TypeScript.

How to use enums inside a class with TypeScript?

To use enums inside a class with TypeScript, we can put it in the same module as the class.

For instance, we write

enum State {
  STATE_IDLE,
  STATE_LOADING,
  STATE_READY,
  STATE_ERROR,
}

export class Image {
  constructor() {}
  public static readonly State = State;
}

to add the Image class that has the static State variable set to the State enum.

Then we can use it with Image.State.

Conclusion

To use enums inside a class with TypeScript, we can put it in the same module as the class.

Categories
TypeScript Answers

How to fix the “Property ‘includes’ does not exist on type ‘string[]'” error with TypeScript?

Sometimes, we want to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript.

In this article, we’ll look at how to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript.

How to fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript?

To fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript, we can add the 'es2017' option to the compilerOptions.lib option in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    //...
    "lib": ["es6", "dom", "es2017"],
    //...
    "target": "es5"
    //...
  }
  //...
}

to add "es2017" into the lib array so that the TypeScript compiler knows that ES2017 features are available for use in our TypeScript project.

Conclusion

To fix the "Property ‘includes’ does not exist on type ‘string[]’" error with TypeScript, we can add the 'es2017' option to the compilerOptions.lib option in tsconfig.json.

Categories
TypeScript Answers

How to use typed arrays with TypeScript?

Sometimes, we want to use typed arrays with TypeScript.

In this article, we’ll look at how to use typed arrays with TypeScript.

How to use typed arrays with TypeScript?

To use typed arrays with TypeScript, we can use the Array type.

For instance, we write

const possessions: Array<Thing> = new Array<Thing>();

to create the possessions variable of type Array<Thing>, which means all entries in the possessions array must be of type Thing.

We can also shorten it to

const possessions: Thing[] = [];

Conclusion

To use typed arrays with TypeScript, we can use the Array type.