2017-08-13 18 views
0

しばらくの間、@ types/jquery 2.0.47を使用していました。私は3.2.11にアップグレードしようとしましたが、ビジュアルスタジオソリューションを構築するときには何もコンパイルされません。何度もエラーが発生していますが、すべて同じではありませんが、間違いなく関連しています。私はtypescript 2.4を使用しています。例:@ type/jquery 2.0.47から3.2.11へのアップグレード

ビルド:プロパティ 'length'はタイプ 'string'に存在しません。番号| string [] '

if ($("#txtSelPrj") && $("#txtSelPrj").val().length > 0) ... 

ビルド:プロパティ 'のstartsWithは、' タイプ「列に存在しません|番号|文字列[] '

var cityVal = $("#City").val(); 
if (cityVal.startsWith('MyCity')) ... 

などです。以下は私のtsconfig.jsonです。なぜこれが起こっているのか分からないことがありますか?

{ 
    "compilerOptions": { 
    "module": "es2015", 
    "moduleResolution": "node", 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "allowSyntheticDefaultImports": true, 
    "noEmitHelpers": true, 
    "noEmitOnError": true, 
    "noImplicitAny": false, 
    "allowUnusedLabels": true, 
    "target": "es5", 
    "sourceMap": true, 
    "strictNullChecks": false, 
    "removeComments": true, 
    "baseUrl": "./scripts", 
    "declaration": false, 
    "paths": { 
    }, 
    "lib": [ 
     "dom", 
     "es6", 
     "scripthost", 
     "es5", 
     "es2015", 
     "es2015.promise", 
     "es2015.iterable" 
    ], 
    "types": [ 
     "angular-ui-bootstrap", 
     "angular", 
     "autolinker", 
     "bootbox", 
     "bootstrap-notify", 
     "bootstrap", 
     "bootstrap-switch", 
     "cldrjs", 
     "globalize", 
     "googlemaps", 
     "google.analytics", 
     "imagesloaded", 
     "jquery.blockui", 
     "jquery.bootstrap.wizard", 
     "jquery", 
     "jqueryui", 
     "jquery.validation", 
     "jsts", 
     "kendo-ui", 
     "masonry-layout", 
     "qtip2", 
     "signalr", 
     "urijs", 
     "moment" 
    ] 
    }, 
    "exclude": [ 
    "node_modules", 
    "bower_components", 
    "build" 
    ], 
    "typeRoots": [ 
    "node_modules/@types", 
    "node_modules/moment" 
    ], 
    "awesomeTypescriptLoaderOptions": { 
    "useBabel": true, 
    "useCache": true 
    }, 
    "compileOnSave": true, 
    "buildOnSave": true 
} 

答えて

1

のドキュメントを見てみると以下に示すようhttp://api.jquery.com/val/ JQueryの3は間違いなくval()への呼び出しからnumberを返します。

enter image description here

だからv3の現在の@タイプ/ jQueryのを正確に反映していますドキュメント。したがって、エラー:lengthnumberに存在しないよう

Build:Property 'length' does not exist on type 'string | number | string[]'.

は間違い正しいです。

修正

あなたはどちらか、例えばその文字列を確保するために、タイプのガードを行うことができますtypeofhttps://basarat.gitbooks.io/typescript/docs/types/typeGuard.html

それとも、型アサーションを使用して怠惰な(と一緒に遊んのように)している場合:https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

+0

私はそれはそれだと思います。正直なところ、このようなアップグレードがアプリケーション全体を壊すことはないと思っていましたが、私はこれをもっとゆっくりと実行しなければならないと思います。どうも。 – Phil

関連する問題