Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tag field narrowing does not work with branded types #171

Open
sakari opened this issue Jan 13, 2021 · 0 comments
Open

tag field narrowing does not work with branded types #171

sakari opened this issue Jan 13, 2021 · 0 comments

Comments

@sakari
Copy link
Collaborator

sakari commented Jan 13, 2021

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
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant