Skip to main content
Codegen is React Native’s code generation system that automatically creates type-safe native interfaces from Flow or TypeScript specifications. It’s a cornerstone of the new architecture, enabling compile-time type safety across the JavaScript/native boundary.

What is Codegen?

Codegen analyzes your JavaScript/TypeScript component and module specs and generates:
  • C++ interfaces - Abstract base classes for TurboModules and Fabric components
  • iOS implementations - Objective-C++ glue code
  • Android implementations - Java/Kotlin JNI bindings
  • Type definitions - Consistent types across platforms
Located in packages/react-native-codegen/, Codegen consists of:
  • Parser - Extracts type information from JS/TS files
  • Schema - Intermediate representation of types
  • Generators - Platform-specific code generators

Why Codegen?

Before Codegen

Manual native module implementation:
Problems:
  • Type mismatches caught only at runtime
  • No guarantee iOS and Android match
  • Boilerplate code for every method
  • Manual serialization logic

With Codegen

Single source of truth:
Codegen automatically generates:
  • C++ abstract interface
  • iOS Objective-C++ implementation template
  • Android Java/Kotlin JNI bindings
  • Type validation at compile time
Codegen ensures type consistency across JavaScript, iOS, Android, and any other platforms. Type errors are caught at build time, not runtime.

Architecture

Schema

Codegen uses an intermediate schema representation defined in src/CodegenSchema.js:
The schema is platform-agnostic and serves as input to all generators.

Pipeline

Parsers

Two parsers support different type systems: Flow Parser (src/parsers/flow/):
TypeScript Parser (src/parsers/typescript/):
Both parse to the same schema format.

Generators

Platform-specific code generators:
  • C++ Generator (src/generators/modules/) - Base TurboModule interfaces
  • iOS Generator (src/generators/components/) - Objective-C++ implementations
  • Android Generator (src/generators/modules/) - Java/Kotlin JNI code

Creating a Codegen Spec

TurboModule Spec

Create a spec file with TypeScript:

Fabric Component Spec

Define a native component:

Commands

Define imperative commands:

Supported Types

Primitives

Objects

Codegen generates C++ structs for object types.

Arrays

Unions (Limited Support)

Complex unions (e.g., string | number) are not fully supported. Use object types with type discriminators instead.

Promises

Callbacks

Codegen Type Utilities

Generated Code

C++ Interface

For TurboModule spec:
Codegen generates:
Your implementation:

iOS (Objective-C++)

Codegen generates protocol and base:
Your implementation:

Android (Java/Kotlin)

Codegen generates abstract class:
Your implementation:

Running Codegen

Codegen runs automatically during build:
Codegen output is in:
  • iOS: ios/build/generated/ios/
  • Android: android/app/build/generated/source/codegen/

Manual

Run Codegen explicitly:
Or via Node:

Configuration

Specify Codegen settings in package.json:
Options:
  • name - Library name for generated code
  • type - "all", "modules", or "components"
  • jsSrcsDir - Directory containing spec files
  • android.javaPackageName - Java package for generated Android code

Best Practices

1. Use TypeScript Over Flow

2. Prefer Readonly Types

3. Use Explicit Numeric Types for Components

4. Provide Defaults for Optional Props

5. Document Complex Types

Troubleshooting

Codegen Not Running

Check:
  1. codegenConfig in package.json is correct
  2. Spec files are in jsSrcsDir
  3. Specs extend TurboModule or use codegenNativeComponent
Force regeneration:

Type Errors

Error: “Unsupported type” Solution: Ensure you’re using supported types. Complex unions and tuples are not supported. Error: “Missing required prop” Solution: Make prop optional with ? or provide default with WithDefault.

Generated Code Not Found

Check output directories:
If empty, Codegen didn’t run. Check build logs.

Mismatched Types

Error: “Cannot convert between types” Solution: Ensure JavaScript types match native implementation:

Advanced Topics

Custom Codegen Types

Extend Codegen with custom parsers:

Multi-Platform Support

Generate code for custom platforms:

Schema Validation

Validate schemas before generation:

Migration Guide

From Legacy Native Modules

Before:
After:

From Manual Type Definitions

Before:
After:

Further Reading

TurboModules

See Codegen in action with TurboModules

Fabric Renderer

Component specs for Fabric

Architecture Overview

How Codegen fits in the architecture

JavaScript Interface

JSI types used by Codegen