Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid __DEV__ declaration conflict with React Native (when "skipLibCheck": false in tsconfig.json) #9386

Merged

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Feb 2, 2022

Should fix #9039, by avoiding shipping any global declaration of the __DEV__ constant (introduced in #8347).

This change allows React Native applications to use @apollo/client with "skipLibCheck": false in their tsconfig.json files. Before this PR, the only workaround for issue #9039 was to set "skipLibCheck": true to avoid rechecking .d.ts files within node_modules/@apollo/client/.

If you need a __DEV__ declaration in your application code, either use @types/react-native (which declares __DEV__ globally) or declare it yourself, in your application:

declare global {
  const __DEV__: boolean | undefined;
}

Since merely using @apollo/client does not guarantee __DEV__ will be defined outside the library (for example, no __DEV__ polyfill is necessary when build tools strip/replace __DEV__ at build time), I think it makes sense to avoid advertising a type for __DEV__ globally, even if there was no conflict to worry about.

@benjamn benjamn self-assigned this Feb 2, 2022
Comment on lines 3 to 20
declare global {
// Despite our attempts to reuse the React Native __DEV__ constant instead of
// inventing something new and Apollo-specific, declaring a useful type for
// __DEV__ unfortunately conflicts (TS2451) with the global declaration in
// @types/react-native/index.d.ts.
//
// To hide that harmless conflict, we @ts-ignore this line, which should
// continue to provide a type for __DEV__ elsewhere in the Apollo Client
// codebase, even when @types/react-native is not in use.
//
// However, because TypeScript drops @ts-ignore comments when generating .d.ts
// files (https://github.com/microsoft/TypeScript/issues/38628), we also
// sanitize the dist/utilities/globals/global.d.ts file to avoid declaring
// __DEV__ globally altogether when @apollo/client is installed in the
// node_modules directory of an application.
//
// @ts-ignore
const __DEV__: boolean | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment explains the situation in a bit more detail.

@benjamn benjamn force-pushed the issue-9039-prevent-__DEV__-conflict-with-React-Native branch from 5d83908 to 8627b19 Compare February 4, 2022 01:11
@benjamn benjamn merged commit a474d57 into release-3.6 Feb 9, 2022
@benjamn benjamn deleted the issue-9039-prevent-__DEV__-conflict-with-React-Native branch February 9, 2022 17:21
benjamn added a commit that referenced this pull request Feb 15, 2022
This commit back-ports PR #9386 from the release-3.6 branch, since the
same changes make sense on main.
benjamn added a commit that referenced this pull request Feb 15, 2022
benjamn added a commit that referenced this pull request Feb 15, 2022
This commit back-ports PR #9386 from the release-3.6 branch, since the
same changes make sense on main.
@benjamn
Copy link
Member Author

benjamn commented Feb 15, 2022

These changes are now available from npm in @apollo/client@3.5.9 (just published).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript error: __DEV__ conflicts with React Native
1 participant