Categories
TypeScript Answers

How to force TypeScript tsc to ignore node_modules folder?

Sometimes, we want to force TypeScript tsc to ignore node_modules folder.

In this article, we’ll look at how to force TypeScript tsc to ignore node_modules folder.

How to force TypeScript tsc to ignore node_modules folder?

To force TypeScript tsc to ignore node_modules folder, we can set the skipLibCheck option to true.

For instance, we write

{
  //...
  "compilerOptions": {
    "skipLibCheck": true
  }
  //...
}

to set the compilerOptions.skipLibCheck option to true in tsconfig.json to skip the node_modules folder check.

Conclusion

To force TypeScript tsc to ignore node_modules folder, we can set the skipLibCheck option to true.

Categories
TypeScript Answers

How to convert a TypeScript enum to object array?

Sometimes, we want to convert a TypeScript enum to object array.

In this article, we’ll look at how to convert a TypeScript enum to object array.

How to convert a TypeScript enum to object array?

To convert a TypeScript enum to object array, we can use the Object.values method.

For instance, we write

enum Colors {
  WHITE = 0,
  BLACK = 1,
  BLUE = 2,
}

const colorValueArray = Object.values(Colors);

to call Object.values with the Colors enum.

This will return an array with the values that we assigned to each enum entry.

So colorValueArray is [0, 1, 2].

Conclusion

To convert a TypeScript enum to object array, we can use the Object.values method.

Categories
TypeScript Answers

How to obtain the return type of a function with TypeScript?

Sometimes, we want to obtain the return type of a function with TypeScript.

In this article, we’ll look at how to obtain the return type of a function with TypeScript.

How to obtain the return type of a function with TypeScript?

To obtain the return type of a function with TypeScript, we can use the ReturnType type.

For instance, we write

type T10 = ReturnType<() => string>;

to get the type of of the () => string function type and assign it to T10.

Therefore T10 is string.

Conclusion

To obtain the return type of a function with TypeScript, we can use the ReturnType type.

Categories
TypeScript Answers

How to import other TypeScript files?

Sometimes, we want to import other TypeScript files.

In this article, we’ll look at how to import other TypeScript files.

How to import other TypeScript files?

To import other TypeScript files, we just use the JavaScript import statement.

For instance, we write

import { ZipCodeValidator } from "./ZipCodeValidator";

const myValidator = new ZipCodeValidator();

to import the ZipCodeValidator constructor from the ZipCodeValidator TypeScript module.

Conclusion

To import other TypeScript files, we just use the JavaScript import statement.

Categories
TypeScript Answers

How to declare a fixed length array in TypeScript?

Sometimes, we want to declare a fixed length array in TypeScript.

In this article, we’ll look at how to declare a fixed length array in TypeScript.

How to declare a fixed length array in TypeScript?

To declare a fixed length array in TypeScript, we can use the Array constructor or create a tuple.

For instance, we write

const arr = new Array<number>(3);
const tuple: [number, number, number] = [1, 2, 3];

to create the arr array which is an array of numbers with new Array<number>(3).

And we create a tuple with the [number, number, number] tuple type which we set to an array with 3 numbers.

Conclusion

To declare a fixed length array in TypeScript, we can use the Array constructor or create a tuple.