How to Add Type-Safe Constants to Swift TurboModules

youtube-cover
Teachers
Oskar Kwaśniewski
Software Developer
@
Callstack

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

Learn how to expose typed, immutable constants from a Swift TurboModule using Codegen, and get full end-to-end type safety between native and JavaScript.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

TurboModules feel powerful but risky?

We help teams use them with confidence in real production apps.

Let’s chat
Link copied to clipboard!
//
Insights

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.

//
insights

Learn more about React Native

//
React Native

We can help you move
it forward!

At Callstack, we work with companies big and small, pushing React Native everyday.

React Native Performance Optimization

Improve React Native apps speed and efficiency through targeted performance enhancements.

On-device AI

Run AI models directly on iOS and Android for privacy-first experiences with reliable performance across real devices.

AI Knowledge Integration

Connect AI to your product’s knowledge so answers stay accurate, up to date, and backed by the right sources with proper access control.

Generative AI App Development

Build and ship production-ready AI features across iOS, Android, and Web with reliable UX, safety controls, and observability.