Categories
TypeScript Answers

How to cast int to enum strings in TypeScript?

Sometimes, we want to cast int to enum strings in TypeScript.

In this article, we’ll look at how to cast int to enum strings in TypeScript.

How to cast int to enum strings in TypeScript?

To cast int to enum strings in TypeScript, we can use square brackets.

For instance, we write

export enum Type {
  Info,
  Warning,
  Error,
  Fatal,
}

to define the Type enum.

Then we use Type[Type.Info] to return type 'Info'.

Conclusion

To cast int to enum strings in TypeScript, we can use square brackets.

Categories
TypeScript Answers

How to access the last element of a TypeScript array?

Sometimes, we want to access the last element of a TypeScript array.

In this article, we’ll look at how to access the last element of a TypeScript array.

How to access the last element of a TypeScript array?

To access the last element of a TypeScript array, we can use the length - 1 index.

For instance, we write

const items: String[] = ["tom", "jeff", "sam"];
console.log(items[items.length - 1]);

to define the items string array.

Then we get the last item from the array with index items.length - 1.

Conclusion

To access the last element of a TypeScript array, we can use the length - 1 index.

Categories
TypeScript Answers

How to fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript?

Sometimes, we want to fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript.

In this article, we’ll look at how to fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript.

How to fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript?

To fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript, w e add a reference to the crypto library in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "crypto": ["node_modules/crypto-js"]
    }
  }
  //...
}

to set the path to the crypto module in the tsconfig.json file in the compilerOptions.paths JSON property.

Conclusion

To fix Module not found: Error: Can’t resolve ‘crypto’ with TypeScript, w e add a reference to the crypto library in tsconfig.json.

Categories
TypeScript Answers

How to fix Error: *.default is not a constructor with TypeScript?

Sometimes, we want to fix Error: *.default is not a constructor with TypeScript.

In this article, we’ll look at how to fix Error: *.default is not a constructor with TypeScript.

How to fix Error: *.default is not a constructor with TypeScript?

To fix Error: *.default is not a constructor with TypeScript, we should make sure we export a class as the default export.

For instance, we write

map_action_file.ts

export default class MapAction implements IMapAction {
  //...
}

to export the MapAction class in map_action_file.ts as a default export.

Then we import it by writing

import MapAction from "./map_action_file";

in another file in the same folder.

Conclusion

To fix Error: *.default is not a constructor with TypeScript, we should make sure we export a class as the default export.

Categories
TypeScript Answers

How to instantiate, initialize and populate an array in TypeScript?

Sometimes, we want to instantiate, initialize and populate an array in TypeScript.

In this article, we’ll look at how to instantiate, initialize and populate an array in TypeScript.

How to instantiate, initialize and populate an array in TypeScript?

To instantiate, initialize and populate an array in TypeScript, we can make a new class.

For instance, we write

class Bar {
  constructor(public length: number) {}
}

const bars = [new Bar(1)];

to define the Bar class that takes the length parameter in the constructor.

And it’ll assign the length value to the length instance property.

Then we define the bars array and assign it an array that has a Bar instance.

Conclusion

To instantiate, initialize and populate an array in TypeScript, we can make a new class.