Skip to content

Fix TS2304 "Cannot find name 'jest'" for TypeScript consumers - #715

Open
IceQ1337 wants to merge 1 commit into
AppsFlyerSDK:developmentfrom
IceQ1337:fix/ts2304-jest-globalthis
Open

IceQ1337 wants to merge 1 commit into
AppsFlyerSDK:developmentfrom
IceQ1337:fix/ts2304-jest-globalthis

Conversation

@IceQ1337

Copy link
Copy Markdown

tsc --noEmit fails for every TypeScript consumer of this package:

node_modules/react-native-appsflyer/index.ts(19,12): error TS2304: Cannot find name 'jest'.

package.json ships raw TypeScript source as the entry point ("main": "index.ts", "types": "index.ts"), so the consumer's tsc compiles index.ts as part of their program. Line 19 references jest, which is never declared in the file. @types/jest is only a devDependency of this package, so it is not installed for consumers, which is correct because a runtime library must not force Jest types on its users.

Accessing jest via globalThis with a typed cast avoids the undeclared identifier entirely, but the runtime behavior is identical, because Jest's global lives on globalThis.

I also considered using declare const jest: unknown;, but that collides with consumers who do actually have @types/jest installed and these would get a "cannot redeclare" error.


Steps to reproduce:

  1. Run npx create-expo-app@latest af-repro --template blank-typescript (any RN + TypeScript project works)
  2. Run npm i react-native-appsflyer@7.0.4
  3. Add import appsFlyer from "react-native-appsflyer"; to the app's entrypoint
  4. Run npx tsc --noEmit

Minimal tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "skipLibCheck": true,
    "noEmit": true,
    "moduleResolution": "bundler",
    "module": "esnext",
    "target": "esnext",
    "types": []
  },
  "include": ["*.ts", "*.tsx"]
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant