使用@type/jQuery2.0.47已经有一段时间了。我试图升级到3.2.11,现在在构建我的visual studio解决方案时,什么都不编译。我得到了很多错误,不完全一样,但肯定是相关的。我正在使用打字本2.4。例如:
Build:属性'length‘不存在于'string number not string[]’类型上。
if ($("#txtSelPrj") && $("#txtSelPrj").val().length > 0) ...Build:属性'startsWith‘不存在于'string number而行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
}发布于 2017-08-14 04:32:59
查看文档:http://api.jquery.com/val/ JQuery 3肯定会从调用val()返回number,如下所示:

因此,当前用于v3的@type/jquery是对文档的准确反映。因此,错误:
Build:属性'length‘不存在于'string number not string[]’类型上。
肯定是正确的,因为length在number上不存在。
修复
您可以执行类型保护以确保它是字符串,例如typeof:https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html
或者如果您很懒(并且喜欢玩),请使用类型断言:https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html
https://stackoverflow.com/questions/45664139
复制相似问题