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.

Categories
TypeScript Answers

How to define type of async function with TypeScript?

Sometimes, we want to define type of async function with TypeScript.

In this article, we’ll look at how to define type of async function with TypeScript.

How to define type of async function with TypeScript?

To define type of async function with TypeScript, we can set the return type of the function to Promise.

For instance, we write

type SearchFn = (subString: string) => Promise<boolean>;

to create the SearchFn type which is set to a function type with the return type of Promise<boolean>.

This means any function with type SearchFn must return a promise that resolves to a boolean.

Conclusipon

To define type of async function with TypeScript, we can set the return type of the function to Promise.

Categories
TypeScript Answers

How to make all properties within a TypeScript interface optional?

To make all properties within a TypeScript interface optional, we can use the Partial type.

For instance, we write

interface Asset {
  id: string;
  internalId: string;
  usage: number;
}

interface AssetDraft extends Partial<Asset> {}

to create the AssetDraft interface that inherits from Partial<Asset>.

Partial<Asset> is a type that makes the properties in the Asset interface optional.

Conclusion

To make all properties within a TypeScript interface optional, we can use the Partial type.

Categories
TypeScript Answers

How to ignore a particular directory or file for TypeScript tslint?

To ignore a particular directory or file for TypeScript tslint, we can add set the linterOptions.exclude option in tslint.json.

For instance, we write

{
  "extends": "tslint:latest",
  "linterOptions": {
    "exclude": ["bin", "lib/*generated.js"]
  }
}

to skip linting on the bin and lin./*generated.js folders.