My code defines various types as follows:
export type Property =
| BooleanProperty
| NumberProperty
| IntegerProperty
| StringProperty
| ObjectProperty
| ArrayProperty;
export interface OneOf {
oneOf: PropertyOrKeyword[];
}
export interface AnyOf {
anyOf: PropertyOrKeyword[];
}
type Keyword = OneOf | AnyOf;
export type PropertyOrKeyword = Property | Keyword;
Later in the code, I have the following snippet:
const val = parent[objName]; // This variable is of type 'PropertyOrKeyword'
if ("oneOf" in parent[objName]) {
const index = val.oneOf.findIndex(
(obj: Property) => obj.title === title
);
val.oneOf[index] = ref(title);
}
However, when hovering over val.oneOf
, an error is displayed:
Error: Property 'oneOf' does not exist on type 'PropertyOrKeyword'.
Property 'oneOf' does not exist on type 'BooleanProperty'.
It seems like there might be an issue with the unioned types causing TypeScript to overlook the existence of the 'oneOf' field. Could it be related to the recursive nature of these types? The use of the in
operator should theoretically help TypeScript infer that val
is of type OneOf
.
Am I overlooking something important here?