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
Read first: Vocabulary, Build time vs. run time
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
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.
- 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:
"codegenConfig": {
"name": "NativeCalculatorSpec",
"type": "modules",
"jsSrcsDir": "specs",
"android": { "javaPackageName": "com.example.calculator" },
"ios": { "modulesProvider": { "Calculator": "RCTCalculator" } }
}| Field | Meaning |
|---|---|
name | Prefix for generated files and classes |
type | "modules", "components" or "all" |
jsSrcsDir | Folder containing the specs |
android.javaPackageName | Java package for the generated Spec classes |
ios.modulesProvider | Maps 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 signature | Becomes |
|---|---|
add(a: number, b: number): number | Synchronous: JS waits for the value |
fetchRate(c: string): Promise<number> | Asynchronous: native gets resolve/reject |
log(msg: string): void | Asynchronous, no result |
subscribe(cb: (v: string) => void): void | Asynchronous, with a callback |
See Sync vs. async for when each is appropriate.
Type mapping#
| Spec (TS) | Android | iOS | C++ |
|---|---|---|---|
number | double | double (NSNumber * when returned) | double |
string | String | NSString * | jsi::String / std::string |
boolean | boolean | BOOL | bool |
| Object type | ReadableMap | Generated C++ struct | jsi::Object |
Array<T> | ReadableArray | NSArray * | jsi::Array |
Promise<T> | Promise parameter | resolve/reject blocks | AsyncPromise |
| Function | Callback | RCTResponseSenderBlock | jsi::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):
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:
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):
@protocol NativeCalculatorSpec <RCTBridgeModule, RCTTurboModule>
- (NSNumber *)add:(double)a b:(double)b;
- (void)fetchRate:(NSString *)currency
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject;
@endThe implementation conforms to it and returns the generated C++ glue:
@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);
}
@endThe C++ glue, which is what JS actually calls:
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.javaand…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:
- Check
package.jsonforcodegenConfig. Missing means the library relies on interop. - Read the specs in
jsSrcsDir: they're the library's real API contract. - Android: find the class extending
Native…Spec. iOS: findgetTurboModule:and the conformed protocol. - If you patch a spec, patch the implementation too and rebuild so the glue is regenerated.
- Libraries with
"includesGeneratedCode": trueship 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.