3

I'm just getting started with Typescript and I'm attempting to use it with a new React Native project. However, I can get TS to compile. I've based my setup off of this article with the exception of using npm @types. I get the correct type hinting in my IDE (Atom), but when I attempt to compile, I get the following error:

node_modules/@types/react-native/index.d.ts(19,24): error TS2307: Cannot find module 'react'.
node_modules/@types/react-native/index.d.ts(222,28): error TS2304: Cannot find name 'Component'.
node_modules/@types/react-native/index.d.ts(1017,15): error TS2304: Cannot find name 'Ref'.
...

My package.json

{
  "name": "healthymeIngage",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "15.3.2",
    "react-native": "0.35.0"
  },
  "jest": {
    "preset": "jest-react-native"
  },
  "devDependencies": {
    "@types/react": "^0.14.41",
    "@types/react-native": "^0.29.36",
    "babel-jest": "16.0.0",
    "babel-preset-react-native": "1.9.0",
    "jest": "16.0.2",
    "jest-react-native": "16.0.0",
    "react-test-renderer": "15.3.2",
    "typescript": "^2.0.3"
  }
}

My tsconfig.json

{
  "compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "jsx": "react",
    "outDir": "build",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "sourceMap": true
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "node_modules/@types/**/*.ts"
  ]
}
1
  • I have the same issue. Did you end up finding a solution? vscode sees all as well, but when I build it fails dramatically Commented Nov 6, 2016 at 22:12

2 Answers 2

2

Since you are using "typescript": "^2.0.3", the error is caused by this reported issue. There is work around for this, which is using "moduleResolution": "node". Be aware that it will not make classic module resolution works for packages afterwards.

@Leone: your fix "module": "commonjs" will not work since React Native ships with ES2015, require() modules will cause run time errors in React Native.

Sign up to request clarification or add additional context in comments.

Comments

0

I changed the module system (the line "module": "es2015") in tsconfig.json file to:

"module": "commonjs",

That way the errors were gone for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.