Skip to content

RFC: @errorCode to provide context for errors for properties #422

Description

It would be nice to be able to inform users when they encounter specific errors related to a property.

In this example use case, we would like to inform the user that a property on a discriminated union can only be set when the discriminator matches the type.

interface Base {
  discriminator: boolean;
  foo: string | never;
}

interface UnionTypeA extends Base {
  discriminator: true;
  foo: string;
}

interface UnionTypeB extends Base {
  discriminator: false;

  /**
   *
   * {@errorCode 2322 | This property can only be set when discriminator is equal to `true`}
   *
   */
  foo: never;
}

type DiscriminatedUnion = UnionTypeA | UnionTypeB;

const bar: DiscriminatedUnion = {
  discriminator: false,
  foo: ""
}

The current error is shown as:
Screenshot 2024-08-23 at 11 35 35 AM

I based the syntax around what I have seen as prior art in the @link tag.

Apologies if this has been suggested or discussed before, I could not find anything from an initial search of issues.

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