错误TS2339:财产'拆分'类型'字符串|中不存在串[]&#39 ;.财产'拆分'类型'字符串[]'上不存在

时间:2017-12-14 12:45:34

标签: typescript

当我构建我的应用程序时,我得到了。 error TS2339: Property 'split' does not exist on type 'string | string[]'. Property 'split' does not exist on type 'string[]'.

如何在字符串或字符串[]上不存在拆分。它看起来像一个字符串数组似乎很奇怪,因为它会在req.headers.authorization.split("")[1]上抛出错误。所以这基本上是:" Bearer(令牌)所以这对我来说似乎是一个分裂的字符串。

我使用的是打字稿版本2.3。

我尝试将tsconfig中的目标更改为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"
}
}

2 个答案:

答案 0 :(得分:20)

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

(<string>req.headers.authorization).split

(req.headers.authorization as string).split

答案 1 :(得分:1)

Brian Ball的解决方案正在工作,用于处理这种情况的另一种方法是方括号[]。

例如

req.headers.authorization[split](//define your conditions here)

例如:

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