打字稿错误与定义文件不匹配
Typescript errors don't match the definition file
我刚切换到 VS2015 和 Typescript 1.7,在使用 JQuery 时遇到一些奇怪的类型错误:
var someObj: <SomeCustomType> = <SomeCustomType>$element.data("source");
此行抛出以下错误:
Type 'JQuery' is not assignable to type 'SomeCustomType'.
但是 JQuery definition file 显示(第 1553 行)此函数签名应该 return <any>
类型:
interface JQuery {
//...
data(key: string): any;
//...
}
相同的代码在具有相同 Typescript 版本的 VS2013 上不会引发任何错误。
编辑:当然,我可以强制转换为 <SomeCustomType><any>
,但这将意味着用本不应该存在的多个强制转换使代码膨胀,这在 [= 的情况下并不令人满意15=] 文件提供了正确的签名。
有什么我遗漏的吗?
编辑:可能我的问题不清楚。
在VS2013中,使用相同的TS版本和d.ts
文件,
$element.data("source")
一切都很好,VS2013 看到一个 <any>
很高兴。
另一方面,VS2015 认为 returned 对象应该是 JQuery
,因此抱怨,好像这个定义文件没有公开提到的签名。
尝试将其转换为您想要的类型:
var someObj: SomeCustomType = <SomeCustomType> $element.data("source");
或更短:
var someObj = <SomeCustomType> $element.data("source");
您需要更新以使用最新的 jquery.d.ts 文件,因为 TypeScript 1.7.5 更严格,而 jquery.d.ts 的早期版本 实际上有一个错误。
这在Relax index signature checks for type any
中有解释
您可以通过 运行
使用 TSD
npm install tsd@next -g
tsd install jquery -ors
我刚切换到 VS2015 和 Typescript 1.7,在使用 JQuery 时遇到一些奇怪的类型错误:
var someObj: <SomeCustomType> = <SomeCustomType>$element.data("source");
此行抛出以下错误:
Type 'JQuery' is not assignable to type 'SomeCustomType'.
但是 JQuery definition file 显示(第 1553 行)此函数签名应该 return <any>
类型:
interface JQuery {
//...
data(key: string): any;
//...
}
相同的代码在具有相同 Typescript 版本的 VS2013 上不会引发任何错误。
编辑:当然,我可以强制转换为 <SomeCustomType><any>
,但这将意味着用本不应该存在的多个强制转换使代码膨胀,这在 [= 的情况下并不令人满意15=] 文件提供了正确的签名。
有什么我遗漏的吗?
编辑:可能我的问题不清楚。
在VS2013中,使用相同的TS版本和d.ts
文件,
$element.data("source")
一切都很好,VS2013 看到一个 <any>
很高兴。
另一方面,VS2015 认为 returned 对象应该是 JQuery
,因此抱怨,好像这个定义文件没有公开提到的签名。
尝试将其转换为您想要的类型:
var someObj: SomeCustomType = <SomeCustomType> $element.data("source");
或更短:
var someObj = <SomeCustomType> $element.data("source");
您需要更新以使用最新的 jquery.d.ts 文件,因为 TypeScript 1.7.5 更严格,而 jquery.d.ts 的早期版本 实际上有一个错误。
这在Relax index signature checks for type any
中有解释您可以通过 运行
使用 TSDnpm install tsd@next -g
tsd install jquery -ors