nfroidure / nfroidure/schema2dts
Manage sub-schema and enum nesting
Open
@nfroidure is already working on this.
Since Sep 24, 2024.
bug
- Dominant language
- TypeScript
- Stars
- 7
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
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 🤷.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.