首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@type/jquery从2.0.47升级到3.2.11

@type/jquery从2.0.47升级到3.2.11
EN

Stack Overflow用户
提问于 2017-08-13 19:34:41
回答 1查看 104关注 0票数 0

使用@type/jQuery2.0.47已经有一段时间了。我试图升级到3.2.11,现在在构建我的visual studio解决方案时,什么都不编译。我得到了很多错误,不完全一样,但肯定是相关的。我正在使用打字本2.4。例如:

Build:属性'length‘不存在于'string number not string[]’类型上。

代码语言:javascript
复制
if ($("#txtSelPrj") && $("#txtSelPrj").val().length > 0) ...

Build:属性'startsWith‘不存在于'string number而行string[]'

代码语言:javascript
复制
var cityVal = $("#City").val();
if (cityVal.startsWith('MyCity')) ...

诸若此类。下面是我的tsconfig.json。为什么会发生这种事,我有什么遗漏吗?

代码语言:javascript
复制
{
  "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 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-14 04:32:59

查看文档:http://api.jquery.com/val/ JQuery 3肯定会从调用val()返回number,如下所示:

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

Build:属性'length‘不存在于'string number not string[]’类型上。

肯定是正确的,因为lengthnumber上不存在。

修复

您可以执行类型保护以确保它是字符串,例如typeofhttps://basarat.gitbooks.io/typescript/docs/types/typeGuard.html

或者如果您很懒(并且喜欢玩),请使用类型断言:https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45664139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档