我创建了一个新项目。我有用于接口、类型等的特殊文件夹。我会有很多,所以我将它们导入到一个文件中,然后从那里导出。当我想导出多个接口时,我会收到一个错误:
error: bundling failed: SyntaxError: C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modulesreact-nativescriptssrctypesPinCodeindex.ts: Exporting local "IPinCodeParams", which is not declared.
13 | }
14 |
> 15 | export { PinStatus, IPinCodeParams }
| ^
at File.buildCodeFrameError (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelcorelibtransformationfilefile.js:261:12)
at NodePath.buildCodeFrameError (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babeltraverselibpathindex.js:157:21)
at getLocalMetadata (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelhelper-module-transformslibnormalize-and-load-metadata.js:281:22)
at child.get.forEach.spec (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelhelper-module-transformslibnormalize-and-load-metadata.js:315:11)
at Array.forEach (<anonymous>)
at programPath.get.forEach.child (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelhelper-module-transformslibnormalize-and-load-metadata.js:307:33)
at Array.forEach (<anonymous>)
at getLocalExportMetadata (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelhelper-module-transformslibnormalize-and-load-metadata.js:294:27)
at getModuleMetadata (C:UsersQWERTYDesktopTestProjects2019TestProjectnode_modules@babelhelper-module-transformslibnormalize-and-load-metadata.js:86:21)
我有这样的文件夹结构:
android
ios
node_modules
src
--->constants
|--->colors.ts
|--->fonts.ts
|--->index.ts
--->router
|--->index.ts
--->screen
|--->Splash
|--->containers
|--->index.ts
|--->SplashContainer.tsx
|--->views
|--->index.ts
|--->SplashView.tsx
|--->index.tsx
|--->index.ts
--->types
|--->PinCode
|--->index.tsx
|--->index.tsx
--->App.tsx
.babelrc
*
*
*
package.json
rn-cli.config.js
tsconfig.json
tslint.json
在文件"PinCode">中,我有一个枚举和一个接口。
当我这样写的时候(好吧):
export enum PinStatus {
New = 'new',
Confirm = 'confirm',
Unlock = 'unlock',
Edit = 'edit',
}
export interface IPinCodeParams {
status: PinStatus;
onComplete: () => void;
onClose?: () => void;
originalCode?: string;
}
但当我这样写的时候(我得到了错误):
enum PinStatus {
New = 'new',
Confirm = 'confirm',
Unlock = 'unlock',
Edit = 'edit',
}
interface IPinCodeParams {
status: PinStatus;
onComplete: () => void;
onClose?: () => void;
originalCode?: string;
}
export { PinStatus, IPinCodeParams };
我创建了一些全新的项目,试图重写这个项目好几次,但都没有结果。无论如何,我终于犯了这个错误。
package.json
{
"name": "TestProject",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "node node_modules/react-native/local-cli/cli.js run-android",
"ios": "node node_modules/react-native/local-cli/cli.js run-ios",
"lint": "yarn tslint && yarn typecheck",
"tslint": "tslint --project tsconfig.json --config tslint.json src/**/*.ts{,x} --format stylish",
"typecheck": "tsc --project tsconfig.json --noEmit",
"postinstall": "typesync",
"type-sync": "typesync"
},
"husky": {
"hooks": {
"pre-commit": "yarn lint",
"pre-push": "yarn lint"
}
},
"dependencies": {
"mobx": "^4.9.2",
"mobx-persist": "^0.4.1",
"mobx-react": "^5.4.3",
"react": "16.8.0",
"react-native": "0.58.4",
"react-native-vector-icons": "^6.2.0",
"react-navigation": "^2.18.1"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-decorators": "^7.3.0",
"@types/babel-core": "6.25.5",
"@types/jest": "24.0.0",
"@types/prettier": "^1.16.0",
"@types/react": "16.8.2",
"@types/react-native": "0.57.34",
"@types/react-native-vector-icons": "^4.6.4",
"@types/react-navigation": "^3.0.1",
"@types/react-test-renderer": "16.8.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.1.0",
"babel-plugin-module-resolver": "^3.1.3",
"husky": "^1.3.1",
"jest": "24.1.0",
"jsc-android": "236355.1.1",
"metro-react-native-babel-preset": "0.51.1",
"mobx-logger": "^0.7.1",
"prettier": "^1.16.4",
"react-native-typescript-transformer": "^1.2.11",
"react-test-renderer": "16.6.3",
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.18.0",
"tslint-eslint-rules": "^5.4.0",
"tslint-react": "^3.6.0",
"typescript": "^3.2.4",
"typesync": "^0.4.1"
},
"jest": {
"preset": "react-native"
}
}
.babelrc
{
"presets": [
"module:metro-react-native-babel-preset",
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/transform-react-jsx-source",
[
"module-resolver",
{
"root": [
"/src"
],
"alias": {
"@api": "./src/api",
"@actions": "./src/actions",
"@components": "./src/components",
"@constants": "./src/constants",
"@hocs": "./src/hocs",
"@managers": "./src/managers",
"@locales": "./src/locales",
"@reducers": "./src/reducers",
"@screens": "./src/screens",
"@static": "./src/static",
"@stores": "./src/stores",
"@styles": "./src/styles",
"@types": "./src/types",
"@utils": "./src/utils"
}
}
],
]
}
tsconfing.json
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"jsx": "react-native",
"baseUrl": "./src",
"outDir": "./dist",
"allowSyntheticDefaultImports": true,
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noEmit": true,
"noImplicitAny": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"paths": {
"@api": [
"api"
],
"@actions": [
"actions"
],
"@components": [
"components"
],
"@constants": [
"constants"
],
"@hocs": [
"hocs"
],
"@managers": [
"managers"
],
"@locales": [
"locales"
],
"@reducers": [
"reducers"
],
"@screens": [
"screens"
],
"@static": [
"static"
],
"@stores": [
"stores"
],
"@styles": [
"styles"
],
"@types": [
"types"
],
"@utils": [
"utils"
]
}
},
}
tslist.json
{
"defaultSeverity": "error",
"extends": [
"tslint-config-prettier",
"tslint-eslint-rules",
"tslint-react",
"tslint:latest"
],
"jsRules": {},
"rules": {
"await-promise": false,
"interface-over-type-literal": false,
"jsx-alignment": true,
"jsx-boolean-value": false,
"jsx-no-bind": true,
"jsx-no-lambda": false,
"max-classes-per-file": false,
"member-access": false,
"member-ordering": false,
"no-constant-condition": false,
"no-duplicate-imports": false,
"no-empty": false,
"no-implicit-dependencies": false,
"no-submodule-imports": false,
"object-literal-sort-keys": false,
"ordered-imports": false,
"quotemark": [
true,
"single",
"jsx-single",
"avoid-escape"
],
"semicolon": [
true,
"always"
],
"trailing-comma": [
true,
{
"singleline": "never",
"multiline": "always"
}
],
"triple-equals": [
true,
"allow-undefined-check"
]
},
"linterOptions": {
"exclude": [
"./src/typings/**/*.d.ts"
]
},
"rulesDirectory": []
}
参考源项目(Github)
为什么会出现此错误?如何修复?
实际上,你可以在自己的问题中写下答案:
export enum PinStatus {
New = 'new',
Confirm = 'confirm',
Unlock = 'unlock',
Edit = 'edit',
}
export interface IPinCodeParams {
status: PinStatus;
onComplete: () => void;
onClose?: () => void;
originalCode?: string;
}