Skip to content

Generate user-defined type guards #40

@christianlavoie

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";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions