Skip to content

tag field narrowing does not work with branded types #171

@sakari

Description

@sakari

branded types cannot be used for narrowing. Say that we have a

TagA:
 type: string
  enum:
    - a
A: 
 oneOf:
  - TaggedA
  -  ...
TaggedA:
  type: object
  properties:
    tag: 
      $ref: TagA

then typescript does not narrow by the tag field as the tag value gets branded as a top level scalar definition.

function foo(a: A): null | Value {
  if (tagged.tag === 'a') {
    return a.value;
  }
  return null;
}

instead need to use instanceof TaggedA or work with ShapeOfTaggedA. Or if possible define TaggedA as

TaggedAd:
...
properties:
  tag: 
    type: string
    enum: a
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions