我有这个代码:
this.$(<string> button.className + ' input').bootstrapToggle('on');
typescript 给了我这个错误:
error TS2339: Property 'bootstrapToggle' does not exist on type 'JQuery<HTMLElement>'.
我成功修复了错误:
(<any> this.$(<string> button.className + ' input')).bootstrapToggle('on');
为什么将元素声明为“any”类型可以解决此错误?
最佳答案
这是因为bootstrapToggle
jquery的声明文件中没有定义。
尝试通过在 .d.ts
下添加此类型的声明来添加此定义。项目中的文件
interface BootstrapToggleOptions {
on?: string;
off?: string;
onstyle?: OnOffStyle;
offstyle?: OnOffStyle;
size?: Size;
width?: number;
height?: number;
}
type OnOffStyle = "default" | "primary" | "success" | "info" | "warning" | "danger";
type Size = "large" | "normal" | "small" | "mini";
interface JQuery {
bootstrapToggle(): JQuery;
bootstrapToggle(options?: BootstrapToggleOptions): JQuery;
}
使用(<any> this.$(<string> button.className + ' input'))
hack,是不可取的,它有点破坏了打字的意义。
关于javascript - 为什么声明类型 <any> 可以解决未知方法的错误? ( typescript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47872805/