Skip to Content
Language reference

Writing TypeScript Native

Write a TypeScript module that a Java, Kotlin, or Swift caller can use directly.

A TypeScript Native module is a .ts file. Use export to give other modules and native callers access to a declaration. This function computes how many pixels remain in a row after a leading inset:

export function availableWidth(width: int, inset: int): int {
  if (inset >= width) return 0
  return width - inset
}

The caller supplies two signed 32-bit integers and receives one back. On the JVM those parameters have Java type int. In Swift they have type Int32. Changing the annotations to number gives both targets a double-precision signature. Try the playground.

Choosing a type

Use number for ordinary JavaScript arithmetic, int for an exact 32-bit quantity, and float when a calculation or a native API calls for single precision. A type annotation can change which overload a call selects. Primitive types covers widths, conversions, and an example where this changes the result.

Writing portable code

Use the standard names for ECMAScript built-ins. Math, Map, ArrayBuffer, and Promise are available without imports. Keep platform APIs in the module that uses them. For example, an Android view and an iOS view can both call availableWidth while the shared calculation stays independent of their UI frameworks.

The portable language takes its standard behavior from ECMAScript 2026. Its native extensions include fixed-width primitives, checked casts, and platform imports. Modules and their dependencies compile ahead of time. Evaluating source strings with eval or a dynamic function constructor is outside the language. Use a function value when a caller supplies behavior.

On this page