[英]How to fix typescript “Property does not exist on type” with union types?
[英]Extended interfaces with union type is throwing "property does not exist on type" error. How to fix this?
看起来 TypeScript 阻止我潜在地访问IChordShape
上的variant
,这不存在。 使用in
的以下更改解决了该问题:
if("variant" in rawShape && rawShape["variant"] === "inversion"){}
这是为了确保属性variant
在访问它之前确实存在
或者
修改IChordShape
以包括variant: string;
并在扩展接口中为variant
保留更具体的类型,以便访问rawShape["variant"]
不会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.