Old architecture vs. new architecturePart 7 of 14

Codegen

How one TypeScript spec becomes Java, Objective-C++ and C++ glue

IntermediateVerified on React Native 0.88Updated 6 December 2026

The big idea#

Codegen is a Node.js tool that runs at build time. It reads your Native*.ts specs and writes the glue code that connects JavaScript to native code on each platform. Your native implementation must extend that glue, so the native compiler enforces that JS and native agree.

Concepts#

Two stages with a neutral format in the middle#

The Codegen pipeline

SpecNative*.ts
ParserTS or Flow
schema.jsonNeutral format
Java glueAndroid *Spec
ObjC++ glueiOS protocol
C++ JSI glueShared *JSI.h

Boxes: Spec (Native*.ts); Parser (TS or Flow); schema.json (Neutral format); Java glue (Android *Spec); ObjC++ glue (iOS protocol); C++ JSI glue (Shared *JSI.h). Arrows: Spec to Parser; Parser to schema.json; schema.json to Java glue; schema.json to ObjC++ glue; schema.json to C++ JSI glue.

The parser only knows TypeScript/Flow; each generator only knows the schema.
  • JavaScript
  • Build time
  • Native
  • C++

After a build, the schema is usually at android/app/build/generated/source/codegen/schema.json.

Telling Codegen where to look#

Codegen only processes packages with a codegenConfig in package.json:

JSON
"codegenConfig": {
  "name": "NativeCalculatorSpec",
  "type": "modules",
  "jsSrcsDir": "specs",
  "android": { "javaPackageName": "com.example.calculator" },
  "ios": { "modulesProvider": { "Calculator": "RCTCalculator" } }
}
FieldMeaning
namePrefix for generated files and classes
type"modules", "components" or "all"
jsSrcsDirFolder containing the specs
android.javaPackageNameJava package for the generated Spec classes
ios.modulesProviderMaps the JS module name to the Objective-C class

Autolinking tells Codegen which installed libraries to process. Since 0.88 Codegen no longer crawls node_modules or follows symlinks, so a locally linked library must be discoverable through autolinking.

The return type decides sync vs. async#

Spec signatureBecomes
add(a: number, b: number): numberSynchronous: JS waits for the value
fetchRate(c: string): Promise<number>Asynchronous: native gets resolve/reject
log(msg: string): voidAsynchronous, no result
subscribe(cb: (v: string) => void): voidAsynchronous, with a callback

See Sync vs. async for when each is appropriate.

Type mapping#

Spec (TS)AndroidiOSC++
numberdoubledouble (NSNumber * when returned)double
stringStringNSString *jsi::String / std::string
booleanbooleanBOOLbool
Object typeReadableMapGenerated C++ structjsi::Object
Array<T>ReadableArrayNSArray *jsi::Array
Promise<T>Promise parameterresolve/reject blocksAsyncPromise
FunctionCallbackRCTResponseSenderBlockjsi::Function
ArrayBuffer (0.88+)com.facebook.react.bridge.ArrayBuffer—jsi::ArrayBuffer

The generated C++ also checks types at run time, so a wrong type throws a JS error instead of passing garbage to native code.

The generated files#

Android (NativeCalculatorSpec.java):

JavaSimplified
public abstract class NativeCalculatorSpec
    extends ReactContextBaseJavaModule implements TurboModule {
  public static final String NAME = "Calculator";

  @ReactMethod(isBlockingSynchronousMethod = true)
  public abstract double add(double a, double b);

  @ReactMethod
  public abstract void fetchRate(String currency, Promise promise);
}

The implementation extends it:

Kotlin
class CalculatorModule(ctx: ReactApplicationContext) : NativeCalculatorSpec(ctx) {
  override fun getName() = NAME
  override fun add(a: Double, b: Double): Double = a + b
  override fun fetchRate(currency: String, promise: Promise) { promise.resolve(0.709) }
}

iOS (NativeCalculatorSpec.h):

Objective-CSimplified
@protocol NativeCalculatorSpec <RCTBridgeModule, RCTTurboModule>
- (NSNumber *)add:(double)a b:(double)b;
- (void)fetchRate:(NSString *)currency
          resolve:(RCTPromiseResolveBlock)resolve
           reject:(RCTPromiseRejectBlock)reject;
@end

The implementation conforms to it and returns the generated C++ glue:

Objective-C
@implementation RCTCalculator
+ (NSString *)moduleName { return @"Calculator"; }
- (NSNumber *)add:(double)a b:(double)b { return @(a + b); }
- (void)fetchRate:(NSString *)currency resolve:(RCTPromiseResolveBlock)resolve
           reject:(RCTPromiseRejectBlock)reject { resolve(@0.709); }
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params {
  return std::make_shared<facebook::react::NativeCalculatorSpecJSI>(params);
}
@end

The C++ glue, which is what JS actually calls:

C++Simplified
static jsi::Value __hostFunction_add(jsi::Runtime &rt, TurboModule &tm,
                                     const jsi::Value *args, size_t count) {
  return static_cast<ObjCTurboModule &>(tm)
      .invokeObjCMethod(rt, NumberKind, "add", @selector(add:b:), args, count);
}

NativeCalculatorSpecJSI::NativeCalculatorSpecJSI(const ObjCTurboModule::InitParams &p)
    : ObjCTurboModule(p) {
  methodMap_["add"]       = MethodMetadata{2, __hostFunction_add};
  methodMap_["fetchRate"] = MethodMetadata{1, __hostFunction_fetchRate};
}

methodMap_ is the key idea: a table from JS method names to C++ functions that convert the arguments and call native code. Calculator.add(2, 3) is a lookup in this table followed by a direct call.

Codegen also writes a C++ base class (NativeCalculatorCxxSpec<T>) for modules written entirely in C++.

Components get more files#

For specs of type "components":

  • Shared C++: Props, EventEmitters, ShadowNodes, ComponentDescriptors, States.
  • Android: …ManagerInterface.java and …ManagerDelegate.java.
  • iOS: RCTComponentViewHelpers.h (typed command helpers).
  • JS: a Babel plugin replaces codegenNativeComponent(...) with a static view config built into the bundle.

Myths vs reality#

Myth

Codegen is a static type checker

Reality: TypeScript type-checks your app. Codegen guarantees that the native side matches the spec, by generating abstract methods the native compiler forces you to implement.

Myth

Codegen doesn't use JSI

Reality: Codegen itself is a build-time script, but the C++ it generates is JSI code: exactly the layer that converts jsi::Value to native types and back.

Myth

You don't write native code with Codegen

Reality: Codegen writes the glue; you still write the implementation.

In practice#

When reading or patching a library:

  1. Check package.json for codegenConfig. Missing means the library relies on interop.
  2. Read the specs in jsSrcsDir: they're the library's real API contract.
  3. Android: find the class extending Native…Spec. iOS: find getTurboModule: and the conformed protocol.
  4. If you patch a spec, patch the implementation too and rebuild so the glue is regenerated.
  5. Libraries with "includesGeneratedCode": true ship their glue; patching the spec alone has no effect until that glue is regenerated.

In one sentence

Codegen parses Native*.ts specs into a neutral schema.json and generates Java, Objective-C++ and C++ glue from it, including the abstract methods you must implement and the methodMap_ that turns a JS call into a direct native call, while the return type decides sync or async and the native compiler enforces that both sides match.