2016-10-22 75 views
2

我刚刚开始使用Typescript,我试图将它与新的React Native项目一起使用。但是,我可以让TS编译。除了使用npm @types之外,我的安装基于this article。我得到正确的类型提示在我的IDE(原子),但是当我尝试编译,我得到以下错误:Typescript 2.0 with React Native

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'. 
... 

我的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" 
    } 
} 

我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" 
    ] 
} 
+0

我有同样的问题。你最终找到了解决方案吗? 012scvscode也可以看到,但是当我构建它时,它会显着失败 – cramhead

回答

0

我改变了模块系统(线"module": "es2015")在tsconfig.json文件:

"module": "commonjs", 

这样错误消失了。

2

由于您使用的是"typescript": "^2.0.3",错误是由此报告的issue引起的。有这方面的工作,这是使用"moduleResolution": "node"。请注意,它不会使经典模块解决方案适用于套件。

@Leone:您的修补程序"module": "commonjs"将无法​​工作,因为React Native随附ES2015,require()模块将导致React Native中的运行时错误。