Categories
TypeScript Answers

How to fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json?

Sometimes, we want to fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json.

In this article, we’ll look at how to fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json.

How to fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json?

To fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json, we need to add all the paths that we want the TypeScript compiler to pick up.

For instance, we write

{
  "compilerOptions": {
    "baseUrl": ".",
    //...
    "paths": {
      "@project/app/modules/*": ["src/modules/*"],
      "@project/server/data/*": ["server/src/json/*"]
    }
  },

  "include": ["./src", "./server"]
}

in tsconfig.json so that the src/modules/* and server/src/json/* directories and their children are all picked up by TypeScript compiler.

Conclusion

To fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json, we need to add all the paths that we want the TypeScript compiler to pick up.

Categories
TypeScript Answers

How to use React createContext in Typescript?

Sometimes, we want to use React createContext in Typescript.

In this article, we’ll look at how to use React createContext in Typescript.

How to use React createContext in Typescript?

To use React createContext in Typescript, we need to define the types for the context props.

For instance, we write

import React, { useReducer } from "react";
import { initialState, IState, reducer } from "./reducer";

interface IContextProps {
  state: IState;
  dispatch: ({ type }: { type: string }) => void;
}

export const AdminStore = React.createContext({} as IContextProps);

export function AdminStoreProvider(props: any) {
  const [state, dispatch] = useReducer(reducer, initialState);

  const value = { state, dispatch };
  return (
    <AdminStore.Provider value={value}>{props.children}</AdminStore.Provider>
  );
}

to create the IContextProps interface and use that as the type for the value prop in AdminStore.Provider by writing

export const AdminStore = React.createContext({} as IContextProps);

IState is a type that we created to add types to the value prop values.

Now the TypeScript compiler should use the types and type errors should clear.

Conclusion

To use React createContext in Typescript, we need to define the types for the context props.

Categories
TypeScript Answers

How to fix the “Cannot find name ‘it'” error in Jest and TypeScript?

To fix the "Cannot find name ‘it’" error in Jest and TypeScript, we add the ts-jest transform to our test files by adding the entry in jest.config.js.

We install ts-jest by running

npm install jest @types/jest ts-jest

For instance, we write

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};

in jest.config.ts to transform "^.+\\.tsx?$" files with ts-jest.

Then in tsconfig.json, we add

{
  "compilerOptions": {
    //...

    "types": ["reflect-metadata", "jest"],
    "typeRoots": ["./types", "./node_modules/@types"]

    //...
  },
  "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],
  "include": ["./src/**/*.tsx", "./src/**/*.ts"]
}

to add the 'jest' type to the types option.

Now the TypeScript compiler should be aware of Jest types.

Categories
TypeScript Answers

How to get the current year using TypeScript?

Sometimes, we want to get the current year using TypeScript.

In this article, we’ll look at how to get the current year using TypeScript.

How to get the current year using TypeScript?

To get the current year using TypeScript, we use the date’s getFullYear method.

For instance, we write

const y = new Date().getFullYear();

to create a new Date instance and call getFullYear to return the 4 digit year number from the date.

Conclusion

To get the current year using TypeScript, we use the date’s getFullYear method.

Categories
TypeScript Answers

How to mock a TypeScript interface with Jest?

Sometimes, we want to mock a TypeScript interface with Jest.

In this article, we’ll look at how to mock a TypeScript interface with Jest.

How to mock a TypeScript interface with Jest?

To mock a TypeScript interface with Jest, we just need to create objects that match the shape of the interface.

For instance, we write

mathlib.multiplier = {
  multiply: jest.fn((a, b) => a * b),
};

to mock the multiply function with jest.fn assuming the multiply method looks like

interface IMultiplier {
  multiply(a: number, b: number): number;
}

The mocked function takes 2 numbers as arguments and returns a number, and we have the same signature and return type in the mocked function.

So no error will be raised.

Conclusion

To mock a TypeScript interface with Jest, we just need to create objects that match the shape of the interface.