2017-12-14 26 views
1

私は自分のアプリケーションをビルドします。 error TS2339: Property 'split' does not exist on type 'string | string[]'. Property 'split' does not exist on type 'string[]'.エラーTS2339:プロパティ 'split'は型 'string |に存在しません。 string [] 'プロパティ 'split'がタイプ 'string []'に存在しません

分割が文字列または文字列[]に存在しない可能性があります。 req.headers.authorization.split( "")[1]にエラーがスローされるため、文字列配列として認識されてしまいました。だから、これは基本的には次のとおりです。「ベアラ(トークン)ので、これは私にスプリットを取得している文字列を思わ

私はtypescriptですバージョン2.3を使用してい

私はes2017にTSconfigのターゲットを変更しようとした

。。。

私はすべてがうまくいきませんでした

をES6とES7するLIBを変えてみました。

何新人のミス私は

Tを作りました彼は私の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/**/*" 
] 
} 

ですこれは、あなたが上splitを起動しようとしているタイプは、値がstringstring[]のいずれであってもよいことを意味し、string|string[]である私の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" 
} 
} 

答えて

0

ですTypeScriptがうまくいくためには、BOTH型はsplitメソッドでなければなりません。あなたはそれが常に文字列になると確信している場合は、どちらかsplitを呼び出す前にstringに定義(可能な場合)またはキャストを更新します。

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

または

(req.headers.authorization as string).split 
関連する問題