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

Manage sub-schema and enum nesting #35

Open
nfroidure opened this issue Sep 24, 2024 · 0 comments
Open

Manage sub-schema and enum nesting #35

nfroidure opened this issue Sep 24, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@nfroidure
Copy link
Owner

nfroidure commented Sep 24, 2024

When having that kind of schema:

{
  "allOf": [{
    "type": "object",
    "properties": {
      "type": { "enum":["type1", "type2"] }
    }
  }, {
    "oneOf": [{
      "type": "object",
      "properties": {
        "type": { "const": "type1" },
        "type1SpecificProp": { "type": "string" }
      }
    }, {
      "type": "object",
      "properties": {
        "type": { "const": "type2" },
        "type2SpecificProp": { "type": "string" }
      }
    }]
  }

The generated types ain't good because "type1" in the "oneOf" type is not considered to be the same that the "type1" value in the enum leading to bad types (possibly due to type branding).

It produces this atm:

declare type ReusedEnumTest = {} & ({
    type: Enums.Type;
} & ({
    type: "type1";
    type1SpecificProp?: string;
} | {
    type: "type2";
    type2SpecificProp?: string;
}));
declare namespace Enums {
    export enum Type {
        Type1 = "type1",
        Type2 = "type2"
    }
}

const x: ReusedEnumTest = {
  type: Enums.Type.Type1, // Type 'Enums.Type' is not assignable to type 'never'
  type1SpecificProp: 'xxxx',
};

And should produce this instead:

declare type ReusedEnumTest = {} & ({
    type: Enums.Type;
} & ({
    type: Enums.Type.Type1;
    type1SpecificProp?: string;
} | {
    type: Enums.Type.Type1;
    type2SpecificProp?: string;
}));
declare namespace Enums {
    export enum Type {
        Type1 = "type1",
        Type2 = "type2"
    }
}

const x: ReusedEnumTest = {
  type: Enums.Type.Type1, // OK
  type1SpecificProp: 'xxxx',
};

The current workaround is to remove the common type property but at the price of having no enum to refer to 🤷.

nfroidure added a commit that referenced this issue Sep 24, 2024
@nfroidure nfroidure self-assigned this Sep 24, 2024
@nfroidure nfroidure added the bug Something isn't working label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant