Typescript ไม่สามารถห้า redux ได้
ฉันกำลังได้รับข้อผิดพลาดดังต่อไปนี้:
node_modules/@types/react-redux/index.d.ts(8,24): error TS2307: Cannot find module 'redux'.
แม้ว่าจะได้ติดตั้งทั้ง react-redux และ redux (package.json) แล้ว:
"dependencies": {
"react": "15.4.2",
"react-native": "0.42.3",
"react-redux": "^5.0.3",
"redux": "^3.6.0"
},
"devDependencies": {
"@types/react": "^15.0.18",
"@types/react-native": "^0.42.9",
"@types/react-redux": "^4.4.38",
"@types/redux": "^3.6.0",
"babel-jest": "19.0.0",
"babel-preset-react-native": "1.9.1",
"jest": "19.0.2",
"react-test-renderer": "15.4.2",
"tslint": "^4.5.1",
"typescript": "^2.2.1"
},
ไฟล์ README.md ใน @types/redux กล่าวว่า:
This is a stub types definition for Redux (https://github.com/reactjs/redux).
Redux provides its own type definitions, so you don't need @types/redux installed!
แต่การถอนการติดตั้งแพ็กเกจ @types/redux ก็ไม่ทำให้เกิดความแตกต่างใด ๆ
มีไอเดียอะไรไหม?
อัปเดต
ฉันคิดว่าการเพิ่ม index.d.ts ไปยังไดเรกทอรี @types/redux (ซึ่งมีเพียง export * from ../../redux/index) จะช่วยแก้ปัญหาได้ แต่ก็ไม่สำเร็จ
นี่คือ tsconfig.json ของฉัน:
{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"jsx": "react",
"outDir": "build",
"rootDir": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"preserveConstEnums": true,
"allowJs": true,
"sourceMap": true
},
"filesGlob": [
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"__tests__",
"index.android.js",
"index.ios.js",
"build",
"node_modules"
],
"compileOnSave": false
}
และฉันแน่นอนว่าลบโฟลเดอร์ @types/redux ออกจาก node_modules แล้ว ฉันกำลังใช้ TypeScript 2.2.1 (ชัดเจนอยู่แล้ว)
อัปเดต 2
โค้ดที่ใช้ redux:
import { combineReducers } from "redux"
export default combineReducers({})
โปรดทราบว่าการเพิ่มเครื่องหมายอัฒภาคหลัง "redux" ไม่ช่วยอะไร
คำตอบ #1
ฉันเจอปัญหาเดียวกันในโปรเจค react-native วิธีแก้ไขนี้ใช้ได้กับฉัน:
คุณต้องเพิ่ม "moduleResolution": "node" เข้าไปในตัวเลือกของคอมไพเลอร์เพื่อรวมคำจำกัดความของประเภทที่เชื่อมโยงในไฟล์ package.json
เอกสารที่นี่ https://www.typescriptlang.org/docs/handbook/module-resolution.html
คำตอบ #2
ฉันได้ย้อนกลับไปที่ @types/[email และนั่นแก้ปัญหาได้ เวอร์ชันนี้รวมไฟล์ index.d.ts สำหรับ redux อาจมีปัญหากับคอมไพเลอร์ TypeScript ที่ไม่ค้นหาไฟล์ index.d.ts ในไดเรกทอรีปกติ node_modules/redux แต่ค้นหาเฉพาะใน node_module/@types เท่านั้น