@ types / jquery从2.0.47升级到3.2.11的问题

时间:2017-08-13 19:34:41

标签: jquery visual-studio typescript definitelytyped

现在使用@ types / jquery 2.0.47一段时间没有任何问题。我试图升级到3.2.11,现在我构建我的visual studio解决方案时根本没有编译。我收到了大量的错误,并非完全相同,但绝对相关。我正在使用打字稿2.4。例如:

构建:属性'string'上不存在属性'length'号码| string []'。

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

Build:属性'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 个答案:

答案 0 :(得分:2)

查看文档:{​​{3}} JQuery 3肯定会在调用number时返回val(),如下所示:

http://api.jquery.com/val/

因此v3的当前@ types / jquery是对文档的准确反映。因此错误:

  

构建:属性'长度'类型'字符串|中不存在号码|串[]'

肯定正确length上不存在number

修复

你可以做一个类型保护,以确保它的字符串,例如typeofenter image description here

或者如果你很懒(和玩游戏一样),请使用类型断言:https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html