Categories
TypeScript Answers

How to set a typed variable to an empty object with TypeScript?

Sometimes, we want to set a typed variable to an empty object with TypeScript.

In this article, we’ll look at how to set a typed variable to an empty object with TypeScript.

How to set a typed variable to an empty object with TypeScript?

To set a typed variable to an empty object with TypeScript, we can cast the empty object to a type with as or brackets.

For instance, we write

type User = {
  Username: string;
  Email: string;
};

const user1 = {} as User;
const user2 = <User>{};

to create a User type and cast the empty objects assigned to user1 and user2 to the User type with as or brackets respectively.

Conclusion

To set a typed variable to an empty object with TypeScript, we can cast the empty object to a type with as or brackets.

Categories
TypeScript Answers

How to mock dependency in Jest with TypeScript?

Sometimes, we want to mock dependency in Jest with TypeScript.

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

How to mock dependency in Jest with TypeScript?

To mock dependency in Jest with TypeScript, we can use the jest.mock method.

For instance, we write

import * as dep from "../dependency";
jest.mock("../dependency");

const mockedDependency = <jest.Mock<typeof dep.default>>dep.default;

it("should do what I need", () => {
  mockedDependency.mockReturnValueOnce("return");
});

to call jest.mock to mock the ./dependency module.

We set the type of it with jest.Mock<typeof dep.default>

And we get the its default export with dep.default.

Conclusion

To mock dependency in Jest with TypeScript, we can use the jest.mock method.

Categories
TypeScript Answers

How to define a singleton in TypeScript?

Sometimes, we want to define a singleton in TypeScript.

In this article, we’ll look at how to define a singleton in TypeScript.

How to define a singleton in TypeScript?

To define a singleton in TypeScript, we can create a class with a getter to return the singleton instance.

For instance, we write

class MyClass {
  private static _instance: MyClass;

  private constructor() {
    //...
  }

  public static get Instance() {
    return this._instance ?? (this._instance = new this());
  }
}

const myClassInstance = MyClass.Instance;

to create the Instance static getter in the MyClass class.

We return this._instance if it’s defined or assign a new MyClass instance to this._instance and return it with

this._instance = new this()

Conclusion

To define a singleton in TypeScript, we can create a class with a getter to return the singleton instance.

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.