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.

Categories
TypeScript Answers

How to derive union type from tuple or array values with TypeScript?

Sometimes, we want to derive union type from tuple or array values with TypeScript.

In this article, we’ll look at how to derive union type from tuple or array values with TypeScript.

How to derive union type from tuple or array values with TypeScript?

To derive union type from tuple or array values with TypeScript, we can use the typeof operator.

For instance, we write

const list = ["a", "b", "c"] as const;
type UnionType = typeof list[number];

to create the list array with some values inside.

Then we use the list to create a union type of the list values with typeof list[number].

And we assign that to UnionType.

Therefore, any UnionType variable must be set to value 'a', 'b', or 'c'.

Conclusion

To derive union type from tuple or array values with TypeScript, we can use the typeof operator.

Categories
TypeScript Answers

How to fix the ‘tsc command not found’ error when compiling TypeScript?

Sometimes, we want to fix the ‘tsc command not found’ error when compiling TypeScript.

In this article, we’ll look at how to fix the ‘tsc command not found’ error when compiling TypeScript.

How to fix the ‘tsc command not found’ error when compiling TypeScript?

To fix the ‘tsc command not found’ error when compiling TypeScript, we can run tsc without installing it with npx.

To run it, we run

npx tsc app.ts

in our project folder.

npx runs tsc directly from the online package source without installing the package.

Conclusion

To fix the ‘tsc command not found’ error when compiling TypeScript, we can run tsc without installing it with npx.