Categories
TypeScript Answers

How to configure custom global interfaces (.d.ts files) for TypeScript?

Sometimes, we want to configure custom global interfaces (.d.ts files) for TypeScript.

In this article, we’ll look at how to configure custom global interfaces (.d.ts files) for TypeScript.

How to configure custom global interfaces (.d.ts files) for TypeScript?

To configure custom global interfaces (.d.ts files) for TypeScript, we add our interfaces directly into a .d.ts file.

For instance, we can add a types.d.ts file and write

interface Dictionary {}
interface Foo {}
interface Bar {}

to add the Dictionary, Foo, and Bar interfaces into the types.d.ts global type definition file.

Conclusion

To configure custom global interfaces (.d.ts files) for TypeScript, we add our interfaces directly into a .d.ts file.

Categories
TypeScript Answers

How to declare an array in TypeScript?

Sometimes, we want to declare an array in TypeScript.

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

How to declare an array in TypeScript?

To declare an array in TypeScript, we can declare them in several different ways.

For instance, we write

const arr1: boolean[] = [];
const arr2: Array<boolean> = [];
const arr3 = [] as boolean[];
const arr4 = <boolean[]>[];
const arr5 = <Array<boolean>>new Array();

to declare a boolean array in 5 different ways.

We can set the variable type to boolean[] or Array<boolean>.

And we can cast the type of the variable to a boolean array with as boolean[], <boolean[]>, or <Array<boolean>>.

Conclusion

To declare an array in TypeScript, we can declare them in several different ways.

Categories
TypeScript Answers

How to create enum like type in TypeScript?

Sometimes, we want to create enum like type in TypeScript.

In this article, we’ll look at how to create enum like type in TypeScript.

How to create enum like type in TypeScript?

To create enum like type in TypeScript, we can use the enum keyword.

For instance, we write

enum Fruit {
  APPLE,
  ORANGE,
}

to create the Fruit enum with the APPLE and ORANGE values.

Conclusion

To create enum like type in TypeScript, we can use the enum keyword.

Categories
TypeScript Answers

How to specify that a class property is an integer with TypeScript?

Sometimes, we want to specify that a class property is an integer with TypeScript.

In this article, we’ll look at how to specify that a class property is an integer with TypeScript.

How to specify that a class property is an integer with TypeScript?

To specify that a class property is an integer with TypeScript, we can use the number type.

For instance, we write

class C {
  public myInt: number;
}

to declare the myInt class property with type number`.

We can set myInt to any number, but we can add checks to check that the number is an integer.

Conclusion

To specify that a class property is an integer with TypeScript, we can use the number type.

Categories
TypeScript Answers

How to use optional chaining with array or functions with TypeScript?

Sometimes, we want to use optional chaining with array or functions with TypeScript.

In this article, we’ll look at how to use optional chaining with array or functions with TypeScript.

How to use optional chaining with array or functions with TypeScript?

To use optional chaining with array or functions with TypeScript, we can use the ?. operator.

For instance, we write

myArray.filter((x) => x.testKey === myTestKey)?.[0];

to return the first entry of the array returned by myArray.filter.

We can use the optional chaining operator to call functions by writing

obj.fn1?.();

Conclusion

To use optional chaining with array or functions with TypeScript, we can use the ?. operator.