There is no question that javascript cannot do what typescript can but it is one of the biggest reforms for developers in javascript programming. These are some of the known pros of using typescript -

  • Giving full confidence in forcing standardization on the type of input & output.
  • Code Readability.
  • Robustness in code.

And what typescript does not -

  • controlling the run-time error.
  • typing automatically.

Now let's review this code without types -

const fetchPlatformConfig = async (req, res) => {

    const platform = req.params?.platforms;
    const userEmail = req.user.email

    const configurations = await configModel
      .find({
        platform,
        email
      })
      .lean();

    return res.json({
      configurations,
    });

};
Code without usage of typescript

And the same code with types -

import { Document } from 'mongoose'; //Importing types
import { Request, Response } from 'express';

interface IUser {
  email: string;
}

interface IAppRequest extends Request {
  user: IUser;
}

interface IAppResponse extends Response {}

interface IConfigModel extends Document {
  name: string;
  platform: string;
  email: string;
  createdAt?: string;
  updatedAt?: string;
}


const fetchPlatformConfig = async (req: IAppRequest, res: IAppResponse) : Promise<IAppResponse> => {

    const platform:string = req.params.platform as string;

    logger.info(`configuration fetchall requested by ${JSON.stringify(req.user.email)}`);

    const configurations: IConfigModelType[] = await configModel
      .find({
        platform,
      })
      .lean();

    return res.json({
      configurations,
    });

};
Code with usage of typescript

Both code does the same job, but the one with type gives the great level of type safety and auto-completion. Identification of type related issues early before going to production. This permits unit tests to fail if there is any type mismatch.

Concluding, to make this possible, each developer at tradeling follows -

  • Typing each variable, function, object & no hacks allowed.
  • We use only those third party libraries which support typescript.
  • Standardization of creating types for variables, functions, objects etc
  • Typing all API's request & response.
  • We type all react components
  • Typescript everywhere...

Are you considering changing from .js to .ts? Add your thoughts in comment section.