-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
fix: generated TS interfaces extend type defined by additional properties #1028
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to provide this PR @mitjacotic! Let me know if you need anything from me to finish it 🙂
@jonaslagoni It slipped my mind, but in the past I had similar issue with mixed types where some keys are known and others not. I found various issues on this topic: microsoft/TypeScript#17867, microsoft/TypeScript#20597 etc. Here are a few points that I've checked: 1Extended type should always indicate that value can be undefined: interface a extends Record<string, string | undefined> {} right now what this PR produces is: interface a extends Record<string, string> {}
const foo: a = {}
a.bar // TS thinks this is a number 2known keys inside the interface need to be of same type/subtype as in the type interface is extending: // OK:
interface a extends Record<string, any> {
known_key: string
}
interface a extends Record<string, string> {
known_key: string
}
interface a extends Record<string, string | number> {
known_key: string
}
// NOT OK, TS errors out:
interface a extends Record<string, number> {
known_key: string
} I believe this last example is the one that should be covered, if it can't be, I don't really see this PR solves anything. 2.1The above problem can kin of be fixed using types instead: type a = { [key: string]: number | undefined } & {
known_key: string;
} But the problem with such type is that a variable can not be defined without force casting: // fails saying that known_string is incompatible with index signature
const foo: a = {
known_key: 'wer'
}
// fails since known_key is required:
const foo: a = {}
// but setters/getters work ok
const foo: a = { } as unknown as bar
a.known_key // is string
a.foo // is number or undefined
a.known_key = 'foo' // OK
a.foo = 123 // OK
a.bar = 'wer' // OK - errors since it should be number Generator currently only generates classes or interfaces, so the 2.1 option with types would require a new option to generate types. |
Hmm, quite a dilemma 😆 Do you see it as feasible to do the following:
This should account for all your cases I think 🤔? Or does one of them not fit into this solution? |
We are gonna re-create the |
Description
When generating TS interfaces right now, additionalPropertes are added as actual property to the intrerface, eg:
this is wrong, instead interface should extend such type:
This PR fixes that:
additionalProperties
propertyrecord
andmap
mapTypeextendInterfaceWithAdditionalProperties
configRelated issue(s)
Resolves #959