React Native Blog May 7, 2018

Using TypeScript With React Native

Article Summary

Getting TypeScript working with React Native used to be a multi-step headache. Now it's one command, but the setup details still matter for production apps.

This React Native team guide walks through integrating TypeScript into React Native projects, covering both the modern streamlined approach and the detailed manual setup. Originally written in 2018 and updated to reflect current best practices, it addresses the practical challenges teams face when adding type safety to mobile apps.

Key Takeaways

Critical Insight

TypeScript integration in React Native is now dramatically simpler with template support, though understanding the underlying configuration helps teams troubleshoot edge cases and optimize for production.

The article reveals why Artsy still uses the manual transformer approach in production despite the easier template option being available.

About This Article

Problem

When developers tried to import React's default and named exports together on one line, they hit a CommonJS interoperability error: 'object prototype may only be an object or null'. The issue came down to how Babel and TypeScript handle modules differently.

Solution

The React Native team found that splitting the import statement worked. Instead of `import React, { Component }` on one line, they documented using two separate imports: `import React` and `import { Component }`. This resolved the module resolution conflict.

Impact

Once developers adopted this import pattern and set up Jest with the ts-jest preprocessor for `.ts` and `.tsx` files, they could run TypeScript React Native apps and execute snapshot tests without needing workarounds.