Categories
TypeScript Answers

How to fix the ‘ts2769: no overload matches this call.’ error with TypeScript?

To fix the ‘ts2769: no overload matches this call.’ error with TypeScript, we should call a function with the arguments that matches one of the signatures of the function.

For instance, we write

function foo(str: "hello"): string;
function foo(str: "world"): string;
function foo(str: "hello" | "world"): string {
  return str;
}

foo("hello");

to define the foo function with multiple signatures.

It accepts the str parameter which can be either 'hello' or 'world'.

And then we call foo with 'hello' to we avoid the error.

If we call foo with something other than 'hello' or 'world', then we get this error.

Categories
TypeScript Answers

How to fix error TS2339: Property ‘x’ does not exist on type ‘Y’ with TypeScript?

To fix error TS2339: Property ‘x’ does not exist on type ‘Y’ with TypeScript, we need to define the property in the interface with the right type.

For instance, we write

interface Images {
  main: string;
  [key: string]: string;
}

const getMain = (images: Images): string => {
  return images.main;
};

to define the Images interface with the main property.

Then we can get the value of the images.main property without an error since the Images interface has the main property and it’s a string.

If the property name and its type matches, then the error should go away.

Categories
TypeScript Answers

How to fix “cannot find module ‘ts-jest/utils'” error with Jest and TypeScript?

To fix "cannot find module ‘ts-jest/utils’" error with Jest and TypeScript, we install the ts/jest package.

To install it, we run

npm i -D jest typescript
npm i -D ts-jest @types/jest

to install the typescript and ts-jest modules with NPM.

typescript is a prerequisite for the ts-jest module.

Then we create a Jest config with

npx ts-jest config:init

Then we run tests with

npm test 

or

npx jest	

With Yarn, we run

yarn add --dev jest typescript
yarn add --dev ts-jest @types/jest

to install the packages.

We run

yarn ts-jest config:init

to create the Jest config.

And we run

yarn test

or

yarn jest

to run the tests.

Categories
TypeScript Answers

How to fix module has no exported member error with TypeScript?

Sometimes, we want to fix module has no exported member error with TypeScript.

In this article, we’ll look at how to fix module has no exported member error with TypeScript.

How to fix module has no exported member error with TypeScript?

To fix module has no exported member error with TypeScript, we make sure we’re importing things that are exported.

For instance, we write

import { SigninComponent } from "./auth/components/signin.component";

to import SigninComponent from ./auth/components/signin.component.ts.

If it’s a default export then we write

import SigninComponent from "./auth/components/signin.component";

We should also make sure the path we’re importing is right.

Conclusion

To fix module has no exported member error with TypeScript, we make sure we’re importing things that are exported.

Categories
TypeScript Answers

How to map Typescript enums?

Sometimes, we want to map Typescript enums.

In this article, we’ll look at how to map Typescript enums.

How to map Typescript enums?

To map Typescript enums, we get the keys and map them.

For instance, we write

const mapped = (Object.keys(MyEnum) as Array<keyof typeof MyEnum>).map(
  (key) => {
    //...
  }
);

to call Object.keys with MyEnum to return an array of enum keys.

Then we call map with a callback to return an array with the mapped entries.

Conclusion

To map Typescript enums, we get the keys and map them.