Support React Native Platform-specific extensions in TypeScript with Import Unions #46822
Open
5 tasks done
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
🔍 Search Terms
react native platform forking
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Support React Native Platform-specific extensions in TypeScript by typing the
import as a type union of the available modules.
In React Native any module file can be replaced by a platform-specific
implementation. For example a
UserProfileImage.tsx
module can have additionalimplementations such as
UserProfileImage.windows.tsx
,UserProfileImage.macos.tsx
andUserProfileImage.ios.tsx
. React Native willresolve to module with the most specificity, and each platform will bundle with
its own file.
This suggestion is to add support in TypeScript to support platform-specific
extensions in such a way that importing a forked module will type it as
a Union Type combining the type of each of the forked modules.
The react native resolution would be enabled as a typescript configuration, together
with a list of platforms to support.
📃 Motivating Example
Say we have the following file modules:
UserProfileImage.tsx
UserProfileImage.windows.tsx
UserProfileImage.macos.tsx
UserProfileImage.android.tsx
Which is imported as follows:
Because
UserProfile
is a forked module, the resulting UserProfile type willnot be the base module (or any of the forks), but a union type of all of
them. Essentially, with tokens matching the filename the type will be as follows:
This will provide type safety for the imported module, and ensure that the code
works in all the represented platforms.
💻 Use Cases
This will be useful for any React Native project using Platform-specific extensions. Currently the easiest way to solve this is to fork TypeScript and modify the module resolution to handle the platforms as a set of file extensions and run TypeScript once per platform, with different parameters.
The text was updated successfully, but these errors were encountered: