Documentation - Utility Types

Useful Utility Generic Type included in Typescript

Type Extraction from CommonJS

When using a CommonJS export where there are named exports and a default class export

To access the type of the class instance and not the constructor, you must use the prototype named export

class Example {
	public integrationConfigurationRepository: IntegrationConfigurationRepository;

	constructor(
    private companyService: typeof CompanyService,
    private projectService: typeof ProjectService,
    private tokenService: InstanceType<typeof TokenService>,
    private urlGenerator: InstanceType<typeof UrlGenerator>,
  ) {
    this.integrationConfigurationRepository = new IntegrationConfigurationRepository();
  }
{

When using a CommonJS factory method

To access the type of the instance the factory creates, you must use ReturnType<typeof Logger> where Logger is the factory method.

class Example {
	public integrationConfigurationRepository: IntegrationConfigurationRepository;

	constructor(
    private logger: ReturnType<typeof Logger>,
    private notificationService: ReturnType<typeof NotificationService>,
	  ) {
    this.integrationConfigurationRepository = new IntegrationConfigurationRepository();
  }
{

Importing the type without the actaul code

When using the import syntax you can import the type ONLY by using the type keyword after import

import type { UserRef } from '../../../generic_company/domain/common';

When the typescript gets compiled to JS, it will NOT import the code from that import, and will instead ignore the import completely since JS cannot use types. It WILL still include the JSDoc typings, allowing you to have code completion without increasing the load time of the file

Defining reusable types within a CommonJS files

To defined a reusable type definition within a CommonJS file, you must use @typedef and define the type as if it's a normal JSDoc comment.

When importing from that CommonJS file in Typescript, it will let you import the type definition explicitly.

/**
 * @typedef {import('generic_company/domain/company/Company') & import('lib/entities/Company.entity').Company} CompanyEntity Company Schema & Company Entity TypeDef
 */

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * @typedef {DEFINE_TYPE_HERE} EXPORTED_TYPE_NAME
 */