Categories
TypeScript Answers

How to declare return types for functions in TypeScript?

Sometimes, we want to declare return types for functions in TypeScript.

In this article, we’ll look at how to declare return types for functions in TypeScript.

How to declare return types for functions in TypeScript?

To declare return types for functions in TypeScript, we put it near the function signature.

For instance, we write

const sum = (a: number, b: number): number => a + b;

to make the sum function to return a value with the number type.

Conclusion

To declare return types for functions in TypeScript, we put it near the function signature.

Categories
TypeScript Answers

How to fix the ‘TypeScript ReferenceError: exports is not defined’ with TypeScript?

To fix the ‘TypeScript ReferenceError: exports is not defined’ with TypeScript, we can set the module option to 'es6' is tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "module": "es6",
    "target": "es5"
  }
  //...
}

to set compilerOptions.module to 'es6' to fix the ‘TypeScript ReferenceError: exports is not defined’ error thrown during compilation.

Categories
TypeScript Answers

How to specify the return type in a TypeScript arrow function?

Sometimes, we want to specify the return type in a TypeScript arrow function.

In this article, we’ll look at how to specify the return type in a TypeScript arrow function.

How to specify the return type in a TypeScript arrow function?

To specify the return type in a TypeScript arrow function, we put a colon and the type after between the signature and the arrow.

For instance, we write

const addTodo = (text: string): AddTodoAction => ({
  type: "ADD_TODO",
  text: text,
});

to make the addTodo function return an object with the AddTodoAction type.

Conclusion

To specify the return type in a TypeScript arrow function, we put a colon and the type after between the signature and the arrow.

Categories
TypeScript Answers

How to import two exported classes with the same name with TypeScript?

Sometimes, we want to import two exported classes with the same name with TypeScript.

In this article, we’ll look at how to import two exported classes with the same name with TypeScript.

How to import two exported classes with the same name with TypeScript?

To import two exported classes with the same name with TypeScript, we can use the as keyword to import a member with a different name.

For instance, we write

import { Class1 } from "../location1/class1";
import { Class1 as Alias } from "../location2/class1";

to Class1 from 2 different modules.

We import the 2nd Class1 as Alias so it won’t clash with the first one.

Then we use Alias to reference the 2nd Class1.

Conclusion

To import two exported classes with the same name with TypeScript, we can use the as keyword to import a member with a different name.

Categories
TypeScript Answers

How to fix the “TypeScript: No index signature with a parameter of type ‘string’ was found on type ‘{ “A”: string; }” error with TypeScript?

To fix the "TypeScript: No index signature with a parameter of type ‘string’ was found on type ‘{ "A": string; }" error with TypeScript, we can use the keyOf operator to use the values of the keys of an object as the type.

For instance, we write

interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "jane",
  age: 20,
};

const getValue = (key: keyof User) => {
  return user[key];
};

to set the getValue function’s key parameter’s type to keyof User.

This means the value of key van be 'name' or 'age'.

Then we can use key in our function to get a value from user.