-
Notifications
You must be signed in to change notification settings - Fork 4
Type Guards for union type nodes report false negatives #135
Comments
Yes, I agree this is an issue. I suggest that we go down the route of (1). We already have the "mapping" in the form of each type's export interface MediaObject extends CreativeWork {
type: 'MediaObject' | 'AudioObject' | 'ImageObject' | 'VideoObject'; but unfortunately those are not available at runtime. But, if we generated enums instead we would get the lookup table at runtime to be able to be used in type guards: enum MediaObjectTypes {
MediaObject = 'MediaObject',
AudioObject = 'AudioObject',
ImageObject = 'ImageObject',
VideoObject = 'VideoObject'
}
export interface MediaObject extends CreativeWork {
type: MediaObjectTypes |
Having though about this some more, I think we should address this at the JSON Schema level, with derived typings in each language. In the schema, we also need to be able to say things like "a CreativeWork, or anything that extends it". For, that we created Instead of manually authoring files such as
|
🎉 This issue has been resolved in version 0.33.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Currently the
isA
andisType
type guards only compare the passedtype
value, and don't take into account broadening of types, and will return false negatives.These become an issue when used as filters in Encoda, mistakenly discarding nodes as being invalid.
Possible solutions:
typeMap
filter variants, and deprecate the usage ofisA
andisType
functions. Drawback being more changes to Encoda, and clunkier ergonomics for defining ad-hoc type .guardsThe text was updated successfully, but these errors were encountered: