Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 错误TS2339:属性';拆分';不存在于类型';字符串|字符串[]和#x27;。财产';拆分';不存在于类型';字符串[]和#x27;_Typescript - Fatal编程技术网

Typescript 错误TS2339:属性';拆分';不存在于类型';字符串|字符串[]和#x27;。财产';拆分';不存在于类型';字符串[]和#x27;

Typescript 错误TS2339:属性';拆分';不存在于类型';字符串|字符串[]和#x27;。财产';拆分';不存在于类型';字符串[]和#x27;,typescript,Typescript,当我构建我的应用程序时,我得到了错误TS2339:类型“string | string[]”上不存在属性“split”。 类型“string[]”上不存在属性“split”。 如何在字符串或字符串[]上不存在拆分。将其视为字符串数组似乎很奇怪,因为它会在req.headers.authorization.split(“”[1]上抛出错误。这基本上是:“持有者(代币),在我看来,这是一个正在分裂的字符串 我使用的是typescript版本2.3 我尝试将Tconfig中的目标更改为es2017 我

当我构建我的应用程序时,我得到了<代码>错误TS2339:类型“string | string[]”上不存在属性“split”。 类型“string[]”上不存在属性“split”。

如何在字符串或字符串[]上不存在拆分。将其视为字符串数组似乎很奇怪,因为它会在req.headers.authorization.split(“”[1]上抛出错误。这基本上是:“持有者(代币),在我看来,这是一个正在分裂的字符串

我使用的是typescript版本2.3

我尝试将Tconfig中的目标更改为es2017

我尝试将lib更改为es6和es7

一切都不管用

我犯了什么新手错误

这是我的tsconfig.json

{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"lib": [
  "es2015",
  "es2017",
  "dom"
],
"paths": {
  "*": [
    "node_modules/*",
    "src/types/*"
  ]
}
},
 "include": [
  "src/**/*"
 ],
 "exclude": [
  "test/**/*"
 ]
}
这是我的package.json

{
"name": "express-typescript-starter",
"version": "0.1.0",
"description": "A starting point for Node.js express apps with TypeScript",
"repository": {
  "type": "git",
  "url": "https://github.com/sahat/hackathon-starter.git"
},
"author": "",
"license": "MIT",
"scripts": {
  "start": "npm run build && npm run watch",
  "build": "npm run build-ts && npm run tslint",
  "serve": "node $NODE_DEBUG_OPTION dist/server.js",
  "watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"nodemon $NODE_DEBUG_OPTION dist/server.js\"",
  "test": "cross-env TEST_REPORT_PATH=./test-reports/ NODE_ENV=test MONGODB_URI=mongodb://localhost:27017/portal-service-test jest --verbose --forceExit",
  "build-ts": "tsc",
  "watch-ts": "tsc -w",
  "tslint": "tslint -c tslint.json -p tsconfig.json",
  "what": "tsc --version",
  "swagger": "swagger-ts-generate --entryFile=./src/server.ts --swaggerDir=./dist --routesDir=./src",
  "clean": "rimraf ./dist"
},
"jest": {
  "testResultsProcessor": "./node_modules/jest-junit-reporter",
  "globals": {
  "__TS_CONFIG__": "tsconfig.json"
  },
  "moduleFileExtensions": [
    "ts",
    "js"
  ],
  "transform": {
    "^.+\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
  },
"testMatch": [
  "**/test/**/*.test.(ts|js)"
],
"testEnvironment": "node"
},
"dependencies": {
"@types/express-jwt": "^0.0.37",
"async": "^2.1.2",
"axios": "^0.16.2",
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.15.2",
"compression": "^1.6.2",
"connect-mongo": "1.3.2",
"cors": "^2.8.4",
"crypto": "^1.0.1",
"dotenv": "^2.0.0",
"errorhandler": "^1.4.3",
"express": "^4.14.0",
"express-jwt": "^5.3.0",
"express-session": "^1.14.2",
"express-validator": "^3.2.1",
"formidable": "^1.1.1",
"jsonwebtoken": "^7.4.1",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.4",
"mock-require": "^2.0.2",
"mongoose": "^4.11.5",
"morgan": "^1.7.0",
"passport": "0.3.2",
"passport-http-jwt-bearer": "^0.1.3",
"q": "^1.5.0",
"qs": "^6.5.1",
"request": "^2.81.0",
"swagger-jsdoc": "^1.9.6",
"swagger-ui-express": "^1.X",
"uuid": "^3.1.0",
"winston": "^2.3.1",
"winston-daily-rotate-file": "^1.4.6",
"yn": "^2.0.0"
},
"devDependencies": {
"@types/async": "^2.0.40",
"@types/body-parser": "^1.16.2",
"@types/chai": "^4.0.4",
"@types/connect-mongo": "^0.0.32",
"@types/cors": "^2.8.1",
"@types/dotenv": "^2.0.20",
"@types/errorhandler": "^0.0.30",
"@types/express": "^4.0.35",
"@types/express-validator": "^3.0.0",
"@types/jest": "^19.2.2",
"@types/lodash": "^4.14.63",
"@types/mongodb": "^2.1.43",
"@types/mongoose": "^4.7.9", 
"@types/morgan": "^1.7.32",
"@types/node": "^7.0.12",
"@types/node-notifier": "^0.0.28",
"@types/nodemailer": "^1.3.32",
"@types/passport": "^0.3.3",
"@types/request": "0.0.42",
"@types/sinon": "^2.3.3",
"@types/sinon-chai": "^2.7.29",
"@types/sinon-express-mock": "^1.3.2",
"@types/supertest": "^2.0.2",
"@types/swagger-jsdoc": "^0.0.1",
"@types/uuid": "^3.4.0",
"@types/winston": "^2.3.3",
"babel-preset-es2015": "^6.24.1",
"chai": "^4.1.2",
"concurrently": "^3.4.0",
"cross-env": "^5.0.5",
"jest": "^19.0.2",
"jest-junit-reporter": "^1.1.0",
"mockgoose": "^7.3.3",
"node-notifier": "^5.1.2",
"nodemon": "^1.11.0",
"rimraf": "^2.6.1",
"sinon": "^3.2.1",
"sinon-chai": "^2.13.0",
"sinon-express-mock": "^1.3.1",
"supertest": "^3.0.0",
"ts-jest": "^19.0.8",
"tslint": "^5.0.0",
"typescript": "2.3.X"
}
}

您试图调用的
split
类型是
string | string[]
,这意味着值可以是
字符串
字符串[]
,为了让TypeScript满意,这两种类型都必须有
split
方法。如果您确信它将始终是字符串,请更新定义(如果可能)或在调用
split
之前强制转换为
字符串

(<string>req.headers.authorization).split
的解决方案是可行的,处理这种情况的另一种方法是方括号[]

比如说

req.headers.authorization[split](//define your conditions here)
对于exmaple:

req.headers.authorization[split](",")
req.headers.authorization[split](",")