Typescript Product Modules

Overview

You can write product modules in TypeScript on the Root Node 24 stack. It's opt-in per module, and you turn it on with a single setting. Once it's on, your code is organised a little differently to the usual single-file JavaScript approach: it's split across files under code/src/, and one code/src/index.ts file exports the functions the platform runs.

TypeScript gives you type safety, editor autocompletion, and the freedom to spread your code across multiple files and folders.

Key Changes

  • Product module code is authored in .ts files instead of a single main.js.
  • Code lives under a new code/src/ directory and can use nested directories.
  • A code/src/index.ts file exports a registeredFunctions object as the entry point.
  • Functions are shared between files using standard import and export statements.
  • The codeFileOrder array is no longer used and is removed from root-config.

Before You Begin

TypeScript is only supported on the Root Node 24 stack, so your product module needs to be on root-node-24 before you can turn it on. If it isn't yet, follow Upgrading to Root Node 24 first.

Enabling TypeScript

There are three changes to make. Do all three together — a half-converted module won't push.

1. Enable TypeScript Product Module Code

In root-config, add typescriptProductModuleCode to settings:

{
  "settings": {
    "typescriptProductModuleCode": true
  }
}
📘

Please note: This setting can only be enabled on the Root Node 24 stack. On any other stack, push will fail with: 'typescriptProductModuleCode' can only be enabled when 'stack' is 'root-node-24'.

2. Remove codeFileOrder

JavaScript modules use a codeFileOrder array in root-config to set the order the code files are combined in. TypeScript modules don't need it — your import statements handle the ordering. Remove the codeFileOrder array from root-config.

3. Restructure the code Directory

Create a src directory inside code, and add an index.ts file inside it:

code/
  src/
    index.ts

code/src/index.ts is the entry point for your TypeScript module. See Registered Functions below.

Registered Functions

code/src/index.ts must export a constant called registeredFunctions. It's an object holding every function the platform runs — quote hooks, application hooks, claim hooks, lifecycle callbacks, and scheduled functions.

You can define these functions inline in index.ts, or import them from other files under code/src/:

import { getQuote, validateQuoteRequest, describeQuoteRequestSchema } from './entities/quotes';
import { getApplication, validateApplicationRequest } from './entities/applications';
import { getPolicy, afterPaymentSuccess, afterPolicyIssued } from './entities/policies';
import { validateClaimRequest, afterClaimApproved } from './entities/claims';

export const registeredFunctions = {
  // Quotes
  getQuote,
  validateQuoteRequest,
  describeQuoteRequestSchema,
  // Applications
  getApplication,
  validateApplicationRequest,
  // Policies
  getPolicy,
  afterPaymentSuccess,
  afterPolicyIssued,
  // Claims
  validateClaimRequest,
  afterClaimApproved,
};
📘

Please note: The platform only runs functions that are on the registeredFunctions object. If you define a function but forget to add it here, it won't be called — so remember to add each new hook to the object.

Organising Your Code

In a JavaScript module, every code file shares one global scope. In a TypeScript module, you use normal import and export statements to share functions between files — so you can organise your code across as many files and folders as you like.

There's no required structure. Any layout works, as long as code/src/index.ts exports the registeredFunctions object. A common approach is to group functions by entity:

code/
  src/
    index.ts               # exports registeredFunctions
    entities/
      quotes.ts
      applications.ts
      policies.ts
      claims.ts
    utils.ts               # shared helpers

Helpers can sit next to your entity files or in a shared file like utils.ts — just import them where you need them. Anything you don't add to registeredFunctions stays private to your module.


Did this page help you?