How to Add Type-Safe Constants to Swift TurboModules

Some values in native code never change, and that makes them perfect candidates for constants. In this episode of Mastering React Native TurboModules, Oskar Kwaśniewski shows how to expose constants from a Swift TurboModule in a way that’s fully type-safe across the native and JavaScript boundary.
Building on the Local Storage example from previous episodes, Oskar walks through defining constants in the TypeScript spec, generating the correct native interfaces with Codegen, and wiring everything together in Swift and Objective-C. Along the way, he explains why TurboModules treat constants differently from regular methods, what Codegen actually generates for you, and how this approach compares to returning untyped dictionaries.
Defining constants in the TypeScript spec
The episode starts in the Codegen spec, where Oskar introduces a LocalStorageConstants type containing a single value: storageKey. He explains why the method name getConstants is special; Codegen looks for it explicitly and generates a dedicated, strongly typed native interface. For comparison, he also adds a regular method returning constants to show how it falls back to an untyped dictionary.
What Codegen generates on the native side
After running Codegen and opening the iOS project, Oskar inspects the generated headers. The difference is immediately visible: getConstants produces a fully typed native structure, while the regular method returns a generic NSDictionary. This is the key to end-to-end type safety: mismatched or missing keys are caught at compile time instead of failing at runtime.
Exposing a Swift constant to Objective-C
On the Swift side, Oskar defines the storage key as a static public let, making it accessible from Objective-C. This small change is enough to allow the native module layer to reference the value safely and expose it through the generated constants interface.
Implementing constants in the Objective-C layer
Most of the work happens in Objective-C. Oskar shows how to store the generated constants structure, initialize it in the module’s initializer, and return it from the required protocol methods. He highlights that TurboModules require implementing both getConstants and constantsToExport, even though only one is actually invoked at runtime; a quirk of the current spec that mirrors React Native’s own internal implementations.
Typed constants vs. untyped dictionaries
To underline the benefit of this approach, Oskar implements the untyped constant method as well. Because it returns a plain dictionary, it compiles even when empty and provides no guarantees on the JavaScript side. In contrast, the typed constants method enforces correct keys and value types at compile time on both sides of the bridge.
Reading constants in JavaScript
Back in JavaScript, Oskar accesses the exported constants and logs the storage key. TypeScript correctly infers the value type, and breakpoints confirm which native methods are actually being called. He also points out a possible optimization used in React Native’s own DeviceInfo module, where constants are cached so they’re only computed once.
Resources
- Oskar Kwaśniewski on X and GitHub
- GitHub repo
- Other episodes in this series
- Tools & concepts
- React Native TurboModules (New Architecture)
- Codegen and TypeScript specs
- Swift and Objective-C interoperability
- Native constants and type safety
Learn more about React Native
Stay up to date with our latest insights on React, React Native, and cross-platform development from the people who build the technology and scale with it daily.
Learn more about React Native

React Native Wrapped 2025: A Month-by-Month Recap of The Year
The first edition of React Native Wrapped looks back at the year RN turned 10 and the ecosystem doubled down on the New Architecture. It provides a month-by-month record of 2025, covering major framework releases, the Legacy Architecture freeze, React 19 integration, and notable developments across tooling, performance, styling, native modules, Expo, and platform adoption.

Building v0 iOS and Fixing React Native Along the Way
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Shipping iOS Live Activities with React: The Voltra Story
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Scaling React Native at Zalando: How Brownfield Migration Paid Off
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.












