fix: replace @ts-ignore with Metro-specific type definitions#832
Conversation
|
Hey thanks for this PR, however I wouldn't want to add this dependency personally. I understand the intention behind this pr, however I don't think this is the right fix since the metro implementation doesn't match the webpack one and we shouldn't rely on any dependency to webpack in my opinion. If anything we should copy the type definitions from expo here However its also low priority in my opinion since this ts-ignore exists in a generated file that can be ignored in eslint |
bcf7930 to
32921da
Compare
32921da to
08a609c
Compare
🦋 Changeset detectedLatest commit: 54992d2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Replace @types/webpack-env dependency with custom Metro type definitions, following the same approach used by Expo. Metro's require.context API intentionally matches webpack's API by design, but using webpack types caused concerns about API differences. This change: - Adds metro-env.d.ts with Metro-specific types in __MetroModuleApi namespace - Exports types via @storybook/react-native/metro-env - Auto-injects reference directive in generated storybook.requires.ts files - Removes both @ts-ignore comments from generated code References: - Expo's approach: https://github.com/expo/expo/blob/main/packages/expo/types/metro-require.d.ts - Metro require.context PR: react/metro#822 - Metro HMR Flow types: https://github.com/facebook/metro/blob/main/packages/metro-runtime/src/polyfills/require.js.flow
08a609c to
f6840bb
Compare
|
Hey @dannyhw, I've rebased this onto the latest
No new dependencies added. Would you be able to take another look when you get a chance? |
|
Hey, thanks for that. I'm just wondering if you could clarify whats the pain point that this solves? Does this tsignore break something for you currently? Just so i can understand better where this is coming from |
|
The main pain point is that the generated Our project runs // eslint.config.ts — ignores
'**/storybook/storybook.requires.ts',This creates a blind spot — if the generated code has an actual type error beyond the missing With proper Metro types via |
|
you actually should ignore the requires file from eslint, its a generated file and its not possible to satisfy every project's lint configuration. I think its valid to aim to remove ts ignore but i don't think it will fix linting problems. |
|
That's a good point — and you're right that ignoring it from ESLint is the practical approach. The challenge though is that The only way to prevent that currently is to patch |
|
Let me know if you have any concerns about the approach or if there's anything else you'd like me to test or adjust before this can be merged. Happy to make changes if needed! |
|
Ok thanks, I will take a proper look tomorrow. Sorry that its been stuck for a while. |
|
Doesn't appear to work for module.hot applied a small fix |
|
available to try on |
|
thanks for your work and patience @YevheniiKotyrlo |
Thank you for your hard work! |
Summary
Removes
@ts-ignorecomment from generatedstorybook.requires.tsby providing proper Metro type definitions.Previous approach (rejected): Added
@types/webpack-envas dependency.New approach (per maintainer feedback): Custom Metro type definitions following the same pattern used by Expo.
Background Research
Metro's
require.contextAPI intentionally matches webpack's API by design (Metro PR #822), but the runtime implementations differ. Rather than using webpack types, this PR provides Metro-specific types.How Expo handles this:
packages/expo/types/metro-require.d.ts__MetroModuleApinamespace to avoid conflicts with other type definitionsWhy not auto-convert from Flow?
Changes
metro-env.d.ts- New file with Metro type definitions__MetroModuleApi.RequireContext- require.context() return type__MetroModuleApi.RequireFunction- require.context() method signature__MetroModuleApi.Hot- module.hot HMR APINodeJS.RequireandNodeModuleinterfacespackage.json@types/webpack-envdependency"./metro-env": "./metro-env.d.ts"exportscripts/generate.js/// <reference types="@storybook/react-native/metro-env" />to generated TypeScript filesReferences
Test Plan
yarn test:generatepasses@ts-ignoreneeded in generated code