A guide on implementing TypeScript with React Native's platform-specific extensions

The issue at hand:

In my react native project, I am using a custom hook that has platform-specific code. I need to import this hook based on the platform in use. When I import it as

import useWifi from 'hooks/use-wifi.android';
, everything works smoothly on Android.

Approach taken so far:

Following React Native's guidelines for platform specific code, I have organized my file structure as shown below:

...
hooks
  ...
  use-wifi.android.ts
  use-wifi.ios.ts
...

However, when I attempt to import it like

import useWifi from 'hooks/use-wifi';
, TypeScript type checking fails and Metro throws the following error:

error: Error: Unable to resolve module `hooks/use-wifi` from `src/screens/home/index.tsx`: hooks/use-wifi could not be found within the project.

As an attempt to resolve this issue, I created a use-wifi.d.ts file with the contents:

export * from './use-wifi.android';
export * from './use-wifi.ios';

This allowed me to pass the TypeScript compiler checks, but Metro still encounters issues during bundling.

Although one solution could be to have a single hooks/use-wifi.ts file and handle the code splitting there, I am exploring if there is a way to achieve this through separate platform-specific files.

Answer №1

Encountered a similar problem and managed to solve it by including the following configuration in the tsconfig.json under "compilerOptions"

"moduleSuffixes": [".ios", ".android", ".native", ""],

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to import from a .storybook.ts file in Vue with TypeScript and Storybook, including how to manage Webpack configurations

I'm currently utilizing Vue with TypeScript in Storybook. Unfortunately, there are no official TypeScript configurations available for using Vue with Storybook. How can I set up Webpack so that I am able to import from another .storybook.ts file with ...

What causes the NavBar to show and hide within a specific range?

Recently, I encountered a perplexing issue with my navbar. It functions correctly except for one strange behavior that has left me baffled. Why does the menu appear when I adjust the width to 631px, but disappear at 600px? And vice versa – why does it wo ...

How to assign a new type to a class in Typescript

I am attempting to re-export a class with an internal type declaration in Typescript. My goal is for the re-exported class to be usable both as a class (with new) and as a type. Below is an example of what I have tried: class XReal { foo() {return 5} } dec ...

Generate dynamic rows with auto-generated IDs on click event in Angular

Can anyone assist me in dynamically adding rows and automatically generating IDs? I am currently using a click event for this task, but when adding a row, I have to input details manually. Is there a way to automate this process? Any help would be greatly ...

Angular 2: Enhancing User Experience with Pop-up Dialogs

Looking to implement a popup dialog that requests user input and returns the value. The popup component is included in the root component, positioned above the app's router outlet. Within the popup component, there is an open() method that toggles a ...

Displaying a div component in React and Typescript upon clicking an element

I've been working on a to-do list project using React and TypeScript. In order to display my completed tasks, I have added a "done" button to the DOM that triggers a function when clicked. Initially, I attempted to use a useState hook in the function ...

"Benefit from precise TypeScript error messages for maintaining a streamlined and organized architecture in your

As I dip my toes into the world of functional programming with typescript for a new project, I am encountering some challenges. In the provided code snippet, which follows a clean architecture model, TypeScript errors are popping up, but pinpointing their ...

Monaco Editor in TypeScript failing to offer autocomplete suggestions

When using a union type as a parameter of a function in Monaco Editor, the suggestions do not appear. However, in VS Code, the suggestions are provided. Is there a setting I need to enable in Monaco to have the same functionality? Although Monaco provides ...

An error occurred as the Serverless Function timed out while attempting to load a dynamic route in Next.js version 13

I have a basic Next.js application with the following route structure: /contentA/ - Static - Initial load: 103 kB /contentA/[paramA]/groups - SSG - Initial load: 120 kB /contentB/[paramA]/[paramB]/[paramC] - SSR (client component) - Initial load: 103 kB ...

Resolve the issue with automatically generating SCSS type definitions (style.d.ts) for Preact within a TypeScript webpack setup

Utilizing webpack with Preact 10.x (nearly identical to React) and TypeScript in the VSCode environment. Following an update from Node version 12 to version 14, there seems to be a problem where *.scss files no longer automatically generate their correspo ...

Facing a react-native-camera issue during android compilation

After attempting to update my react native project to the latest version (0.59.2), I encountered an issue when trying to execute react-native run-android with this error: Could not establish the dependencies of task ':app:preDebugBuild'. > Co ...

What is the most effective method of testing with jest to verify that a TypeScript Enum list contains all the expected string values?

Recently, I created a list of enums: export enum Hobbies { Paint = 'PAINT', Run = 'RUN', Bike = 'BIKE', Dance = 'DANCE' } My goal is to iterate through this list using Jest and verify that all the string ...

When defining a stripe in TypeScript using process.env.STRIPE_SECRET_KEY, an error of "string | undefined" is encountered

Every time I attempt to create a new stripe object, I encounter the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string& ...

What is the best way to pass a URL as a prop in Next.js without encountering the issue of it being undefined

Within my file (handlers.ts), I have a function designed to post data to a dynamic API route while utilizing the app router. This function requires values and a URL for fetching. However, when I pass the URL as a prop like this: http://localhost:3000/unde ...

The object is not defined when trying to evaluate `_reactNavigation.ThemeColors.light`

Currently, I am working on atome and utilizing NPM with react-native. Encountered an error while trying to launch the application: Received a TypeError stating that undefined is not an object when evaluating '_reactNavigation.ThemeColors.light&apo ...

What could be causing the failure to typecheck the sx prop?

Trying to implement sx prop-based styling in a React project. Looking to utilize the theme using a theme getter. While styles render correctly in the browser, TypeScript raises errors and understanding the type ancestry is proving challenging. Working e ...

Dynamically Altering the Visibility of a Button Within a Component

I'm utilizing React-Navigation, but prior knowledge of it isn't necessary. Here's my header Component: const Header = (props) => { return ( <View style={headerContainer}> <View> <Button onPress={() => prop ...

"Encountering an issue with Expo CLI and ADB: Unable to establish connection with daemon

Having trouble setting up expo CLI and ADB on my Windows 10 64-bit PC with the Genymotion emulator Google Pixel 3. When I try to run "on android device/emulator from expo cli," I encounter the following logs: Cannot initiate project on Android: Issue r ...

When executing npm release alongside webpack, an error is triggered

Currently, I am following a tutorial provided by Microsoft. You can access it through this link: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr-typescript-webpack?view=aspnetcore-3.1&tabs=visual-studio However, when attempting to run ...

Obtain the default/initial argument type of typescript for extension purposes

Currently, I am in the process of developing code that automatically generates type hints based on function calls related to GraphQL Nexus tasks. In one of its functions, it requires an anonymous type constructed from the attributes generated automaticall ...