2017-08-13 19 views
0

一直在使用@ types/jquery 2.0.47現在沒有任何問題。我試圖升級到3.2.11,現在當我構建我的visual studio解決方案時,沒有任何編譯。我收到了大量的錯誤,但並不完全相同,但絕對相關。我正在使用typescript 2.4。例如:問題與@ types/jquery從2.0.47升級到3.2.11

構建:屬性「長度」不存在於類型'string |數字|串[]'。

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

編譯:房產 'startsWith' 不會對類型「字符串存在|數字| string []'

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是準確地反映文檔。因此,錯誤:

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

絕對是正確的length不上number存在。

修復

您可以做一個類型的後衛,以確保其如一個字符串typeofhttps://basarat.gitbooks.io/typescript/docs/types/typeGuard.html

或者,如果你很懶(和喜歡玩)使用類型斷言:https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

+0

我想這是它然後。我真的沒有想到像這樣的升級會破壞我的整個應用程序,但我想我必須慢慢地進入這一個。謝謝。 – Phil