Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2339: Property 'split' does not exist on type 'string | string[]'. Property 'split' does not exist on type 'string[]'

Tags:

When I build my application I get. error TS2339: Property 'split' does not exist on type 'string | string[]'. Property 'split' does not exist on type 'string[]'.

How can a split not exist on a string or string[]. It seems weird it sees it as a string array because it throws the error on a req.headers.authorization.split(" ")[1]. So this is basically: "Bearer (the token) so this seems to me a string that is getting split.

I'm using typescript version 2.3.

I tried changing the target in tsconfig to es2017.

I tried changing the lib to es6 and es7

All didn't work.

What rookie mistake did I make

This is my 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/**/*"  ] } 

This is my 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" } } 
like image 543
DePrestige Avatar asked Dec 14 '17 12:12

DePrestige


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Does not exist on type string TypeScript?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string. Here is an example of how the error occurs.

Does not exist on type string TS 2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };

Does not exist on type void?

The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.


1 Answers

The type you are trying to invoke split on is string|string[], which means that value may be either a string or a string[], in order for TypeScript to be happy, BOTH types must have a split method. If you are confident that it will always be a string, either update the definition (if possible) or cast to a string before calling split:

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

or

(req.headers.authorization as string).split 
like image 52
Brian Ball Avatar answered Sep 20 '22 13:09

Brian Ball