javascript – 覆盖Typescript d.ts文件中定义的覆盖接口属性类型
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 覆盖Typescript d.ts文件中定义的覆盖接口属性类型,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1480字,纯文字阅读大概需要3分钟。
内容图文
有没有办法更改typescript中* .d.ts中定义的接口属性的类型?
例如:
x.d.ts中的接口定义为
interface A {
property: number;
}
我想在我写的typescript文件中更改它
interface A {
property: Object;
}
甚至这会起作用
interface B extends A {
property: Object;
}
这种方法会起作用吗?当我尝试使用我的系统时,它无法正常工作.只是想确认它是否可能?
解决方法:
您无法更改现有属性的类型.
您可以添加属性:
interface A {
newProperty: any;
}
但改变一种现有的类型:
interface A {
property: any;
}
导致错误:
Subsequent variable declarations must have the same type. Variable
‘property’ must be of type ‘number’, but here has type ‘any’
您当然可以拥有自己的界面,扩展现有界面.在这种情况下,您只能将类型覆盖为兼容类型,例如:
interface A {
x: string | number;
}
interface B extends A {
x: number;
}
顺便说一下,您可能应该避免使用Object作为类型,而是使用any类型.
The any type is a powerful way to work with existing JavaScript,
allowing you to gradually opt-in and opt-out of type-checking during
compilation. You might expect Object to play a similar role, as it
does in other languages. But variables of type Object only allow you
to assign any value to them – you can’t call arbitrary methods on
them, even ones that actually exist:
let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
内容总结
以上是互联网集市为您收集整理的javascript – 覆盖Typescript d.ts文件中定义的覆盖接口属性类型全部内容,希望文章能够帮你解决javascript – 覆盖Typescript d.ts文件中定义的覆盖接口属性类型所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。