2016-10-22 79 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中的運行時錯誤。