有没有办法更改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'.
原文地址:https://codeday.me/bug/20190928/1827360.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。