Categories
TypeScript Answers

How to parse a JSON object to a TypeScript object?

Sometimes, we want to parse a JSON object to a TypeScript object.

In this article, we’ll look at how to parse a JSON object to a TypeScript object.

How to parse a JSON object to a TypeScript object?

To parse a JSON object to a TypeScript object, we use the JSON.parse method.

For instance, we write

interface Employee {
  departmentId: number;
  permissionsId: number;
  maxWorkHours: number;
  employeeId: number;
  firstName: string;
  lastName: string;
}

const jsonObj: any = JSON.parse(employeeString);
const employee: Employee = <Employee>jsonObj;

to define the Employee interface.

Then we call JSON.parse with the employeeStrigng JSON object string to parse it.

Next, we assign the jsonObj object to employee to and use <Employee> to cast it to an Employee object.

Conclusion

To parse a JSON object to a TypeScript object, we use the JSON.parse method.

Categories
TypeScript Answers

How to explicitly specify types for Express’ application, request, response, etc. with TypeScript?

Sometimes, we want to explicitly specify types for Express’ application, request, response, etc. with TypeScript.

In this article, we’ll look at how to explicitly specify types for Express’ application, request, response, etc. with TypeScript.

How to explicitly specify types for Express’ application, request, response, etc. with TypeScript?

To explicitly specify types for Express’ application, request, response, etc. with TypeScript, we can get the types from the express module.

For instance, we write

import express = require("express");
//...

app.get("/", function (req: express.Request, res: express.Response) {
  //...
});

to require the express module with

import express = require("express");

Then we set the types for req and res to express.Request and express.Response respectively.

Conclusion

To explicitly specify types for Express’ application, request, response, etc. with TypeScript, we can get the types from the express module.

Categories
TypeScript Answers

How to fix the “No overload matches this call. Type ‘string’ is not assignable to type ‘Signals'” error in TypeScript?

Sometimes, we want to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript.

In this article, we’ll look at how to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript.

How to fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript?

To fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript, we can use a type guard to filter out the values that don’t have the Signals type.

To do this, we write

enum Signals {
  SIGHUP = 1,
  SIGINT = 2,
  SIGTERM = 15,
}

Object.values(Signals)
  .filter((s): s is NodeJS.Signals => typeof s !== "number")
  .forEach((signal) => {
    process.on(signal, {
      //...
    });
  });

to get the enum values from the Signals enum with Object.values.

Then we call filter with a type guard function that filters out all the non-number values from the array returned by Object.values.

Finally, we call forEach on the array returned by filter.

Conclusion

To fix the "No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’" error in TypeScript, we can use a type guard to filter out the values that don’t have the Signals type.

Categories
TypeScript Answers

How to use the spread operator with function arguments with TypeScript?

Sometimes, we want to use the spread operator with function arguments with TypeScript.

In this article, we’ll look at how to use the spread operator with function arguments with TypeScript.

How to use the spread operator with function arguments with TypeScript?

To use the spread operator with function arguments with TypeScript, we can use function overloading to make a function accept different kinds of arguments without compiler errors.

For instance, we write

function foo(...args: number[]): void;
function foo(x: number, y: number, z: number) {
  console.log(x, y, z);
}

const args: number[] = [0, 1, 2];
foo(...args);

to define the foo function to takes an unlimited number of number arguments or 3 number arguments.

Then we can define the args number array and spread that into the foo function as arguments without compiler errors.

Conclusion

To use the spread operator with function arguments with TypeScript, we can use function overloading to make a function accept different kinds of arguments without compiler errors.

Categories
TypeScript Answers

How to import all modules from a directory in TypeScript?

Sometimes, we want to import all modules from a directory in TypeScript.

In this article, we’ll look at how to import all modules from a directory in TypeScript.

How to import all modules from a directory in TypeScript?

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported.

For instance, we write

my-module/index.ts

export { default as A } from "./a";
export { default as B } from "./b";

to import the default exports from modules a and b.

Then we can import all the exported modules from my-module/index.ts by writing

import * as whatever from "./my-module";

to import all the exports from my-module/index.ts.

Conclusion

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported.