-
Notifications
You must be signed in to change notification settings - Fork 394
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
Broken generation with additionalProperties #356
Comments
From ts handbook
|
So this is more broken than I thought. Basically the index type in typescript enforces on all existing properties, while in json additionalProperties spec only applies to things not listed in properties. |
additionalProperties probably needs to always translate to [k: string]: unknown; |
This is a bug, and we should find a better way to handle this case. One option is to always emit index signatures as However, I'm not sure this is always desirable, and if it wasn't for the A better approach may be to change the way we emit index signatures, to always emit them as an intersection type: export type FormObject = {[k: string]: string} & {message?: string}
declare const x: FormObject
x.a.toUpperCase() // OK
x.message.toUpperCase() // Error: Object is possibly 'undefined' I'd love input from others. Are there cases this might not handle? Examples welcome. |
Unfortunately it's also broken for required properties that simply have a different type. This is invalid because the index type must contain the union of all listed types. So really I think the only option is to put the index type as 'unknown'. ...
export interface FormObject {
message: number;
[k: string]: string;
} generated by: {
"allOf": [{ "$ref": "#/definitions/FormObject" }],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"FormObject": {
"properties": {
"message": {
"type": "number"
}
},
"required": ["message"],
"additionalProperties": {
"type": "string"
},
"type": "object"
}
}
} |
Yeah intersection might work |
The error in the body of the original issue is a duplicate of #235 which was resolved by implementing (which I agree should ideally be on by default).
This should be desirable as IMO it gives the more accurate type - while If a set of properties are known on the object, then there are a few ways of handling that - most powerfully you can use declaration merging, which is how we handle this over at DefinitelyTyped i.e for
This doesn't require the global or namespace, so using your example:
Yes, it prevents declaration merging as that can only be done with interfaces. This is why in DefinitelyTyped we tend to use empty interfaces (or interfaces with an index) as it lets you do cool things in apps like defining known headers for webhook lambdas:
|
Facing the same issue for the {
"allOf": [{ "$ref": "#/definitions/FormObject" }],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"FormObject": {
"properties": {
"message": {
"type": "number"
}
},
"required": ["message"],
"patternProperties": {
"^[a-zA-Z0-9]{1,255}$": {
"type": "string"
}
},
"type": "object"
}
}
} is generating export interface FormObject {
message: number;
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^[a-zA-Z0-9]{1,255}$".
*/
[k: string]: string;
} However, using union types rather than intersection seems to do the trick. Am I missing something or would you consider treating additionalProperties/patternProperties as a union like shown below ? export type FormObject =
| { message: number; }
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^[a-zA-Z0-9]{1,255}$".
*/
| { [k: string]: string; } |
That type is not representative for the JSON schema though, no? (I'm new to JSON schema so I might be interpreting it wrong) |
Hi! Any chance of reviewing PR #383 which might close this issue? |
Generated via v10.0.0:
Schema
I left the maxLength properties in so you can see why I wrote the schema like this.
The text was updated successfully, but these errors were encountered: