CS-log-dark.svg

JavaScript to TypeScript Migration Tool

The JavaScript to TypeScript Migration Tool converts JavaScript code built with the Contentstack JavaScript SDK into accurate TypeScript.

It works by:

  • Analyzing SDK usage patterns such as content-type queries, entry operations, and chained methods
  • Applying Contentstack-specific TypeScript rules
  • Generating validated output that developers can review and execute

This guide provides an overview of the conversion engine and explains the steps involved in transforming JavaScript input into TypeScript output.

Key Capabilities

  • Consistent JS to TS conversion using a fixed-seed evaluation model, ensuring deterministic and reproducible output
  • Supports conversion for more than 50 Delivery SDK methods
  • The tool uses multiple rule-based approaches to analyze and convert code
  • Execution support for JavaScript and TypeScript in a controlled sandbox
  • TypeScript output structured according to the Delivery SDK method and typing conventions

JS-to-TS Conversion Process

The following steps outline how the conversion engine processes JavaScript input and generates the corresponding TypeScript output.

1. Detecting SDK patterns

The converter begins by scanning the JavaScript input to identify the following SDK patterns:

  • Content types, entries, queries, assets, taxonomies, and sync operations
  • Chained API patterns
  • Embedded query operations
  • Parameters, arguments, and schema references

2. Mapping types

In this step, the converter maps the detected SDK patterns to their TypeScript equivalents. This includes:

  • Normalizing JavaScript-style patterns to Delivery SDK TypeScript conventions
  • Adding generics to methods such as fetch<T> and find<T>
  • Inserting required TypeScript imports for Delivery SDK usage

3. Structuring the converted code

In this step, the converter organizes the transformed operations into a structured TypeScript format by:

  • Restructuring JavaScript-style method chains into typed, modular sequences
  • Converting uppercase method entry points to standard Delivery SDK naming conventions
  • Aligning method order and structure with Delivery SDK TypeScript patterns

4. Validating the output

In this step, the converter verifies that the generated TypeScript meets the required standards by:

  • Ensuring correct syntax
  • Checking type compatibility
  • Confirming alignment with the expected Delivery SDK version.

Using the JS-to-TS Migration Tool

To convert JavaScript code to TypeScript, perform the following steps:

  1. Copy and paste your Contentstack JavaScript SDK code into the input field, then click Convert to TypeScript.
  2. To run and compare both versions in the sandbox, enter your stack details, including your API Key, Environment, and Delivery Token.

Example Conversion Scenarios

The following examples show how the tool transforms common JavaScript patterns into structured TypeScript. Code is represented as placeholders.

Example 1: Simple entry fetch

JavaScript Input:

Stack.ContentType('page').Entry('uid').fetch()

TypeScript Output:

import contentstack, { BaseEntry } from '@contentstack/delivery-sdk'

interface Page extends BaseEntry {}

const result = await stack
  .contentType('page')
  .entry('uid')
  .fetch<Page>();

Example 2: Complex query conversion

JavaScript Input:

Stack.ContentType('blog')
  .Entry()
  .Query()
  .where('title', 'test')
  .ascending('created_at')
  .limit(10)
  .find()

TypeScript Output:

import contentstack, { BaseEntry } from '@contentstack/delivery-sdk'

interface Blog extends BaseEntry {}

const query = stack
  .contentType('blog')
  .entry()
  .query();

query.where('title', 'test');
query.orderByAscending('created_at');
query.limit(10);

const response = await query.find<Blog>();

Example 3: Taxonomy query conversion

JavaScript Input:

Stack.ContentType('page').Query().equalAndBelow('category', 'tech', 2)

TypeScript Output:

import contentstack, { TaxonomyQueryOperation } from '@contentstack/delivery-sdk'

const data = await stack
  .contentType('page')
  .query()
  .where(
    'category',
    TaxonomyQueryOperation.EQ_BELOW,
    'tech',
    { levels: 2 }
  )
  .find();

Example 4: Sync API conversion

JavaScript Input:

Stack.sync({ init: true })

TypeScript Output:

const data = await stack.sync();

Limitations

The JavaScript to TypeScript Migration Tool currently does not support the following:

  • Custom helper functions defined outside SDK chains
  • Non-Delivery SDK JavaScript patterns
  • Dynamic runtime-generated queries
  • Deprecated Delivery SDK methods
  • Inline expressions in SDK chains
  • Conditional or dynamically built method chains
  • Mixed TypeScript/JavaScript inputs
  • Import-path resolution
  • Wrapper libraries or SDK abstractions