Skip to content

Generate user-defined type guards #40

Description

@christianlavoie

In cases where a sum type is transcribed as a series of interfaces and a union type:

export type U = A | B | C;

export interface A {
  tag: "A";
  contents: ...
}

export interface B {
  tag: "B";
  contents: ...
}

export interface C {
  tag: "C";
  contents: ...
}

Type predicates (https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) would be useful:

function isA(u: A | B | C): U is A {
    return (u as A).tag == "A";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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