Skip to content

fix(types): extends check of generic in WithSdkStreamMixin #4119

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

Merged
merged 3 commits into from
Oct 31, 2022

Conversation

Lewenhaupt
Copy link
Contributor

Fix an issue where WithSdkStreamMixin would apply to more properties than intended due to Distributive Conditional Types.

Issue

#4111

Description

Fix an issue where WithSdkStreamMixin would apply to more properties than intended due to Distributive Conditional Types.

Testing

Tested locally with client-s3 and it's GetObjectCommand-output which now correctly only applies the mixin on "Body".


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Fix an issue where WithSdkStreamMixin would apply to more properties than intended due to Distributive Conditional Types.
@Lewenhaupt Lewenhaupt requested a review from a team as a code owner October 28, 2022 16:52
@Lewenhaupt
Copy link
Contributor Author

@trivikr Does your builds get cached? Seems it's the same build.

Is the title better now?

@kuhe kuhe self-requested a review October 28, 2022 17:59
@@ -92,7 +92,7 @@ export type SdkStream<BaseStream> = BaseStream & SdkStreamMixin;
* with the SdkStreamMixin helper methods.
*/
export type WithSdkStreamMixin<T, StreamKey extends keyof T> = {
[key in keyof T]: T[key] extends T[StreamKey] ? SdkStream<T[StreamKey]> : T[key];
[key in keyof T]: [T[key]] extends [T[StreamKey]] ? SdkStream<T[StreamKey]> : T[key];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[key in keyof T]: [T[key]] extends [T[StreamKey]] ? SdkStream<T[StreamKey]> : T[key];
[key in keyof T]: key extends StreamKey ? SdkStream<T[StreamKey]> : T[key];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type Stream = {} | undefined;

type Data = {
    undef: undefined
    num: number
    str: string
    stream: Stream
}

type Mixin<T> = T & {
    someAddedMethod(): void
}

// for v1/v2, the match is on the type of the key indicated. This is too wide if the Stream type includes undefined.
type ApplyConditionalTypeMappingV1<T, Key extends keyof T> = {
    [key in keyof T]: T[key] extends T[Key] ? Mixin<T[Key]> : T[key];
}

type ApplyConditionalTypeMappingV2<T, Key extends keyof T> = {
    [key in keyof T]: [T[key]] extends [T[Key]] ? Mixin<T[Key]> : T[key];
}

// this applies the type mixin only to the specified key.
type ApplyConditionalTypeMappingV3<T, Key extends keyof T> = {
    [key in keyof T]: key extends Key ? Mixin<T[Key]> : T[key];
}

const data: ApplyConditionalTypeMappingV3<Data, 'stream'> = {
    num: 1,
    str: 'a',
    stream: undefined,
    undef: undefined,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this is more robust

Copy link
Contributor Author

@Lewenhaupt Lewenhaupt Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated docs, however this does change the behaviour and I'm not sure if the change from matching multiple keys to only a single key is expected (even though the signature of the mixin definitely indicates it only should match one key). But I'll leave that up to you maintainers :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there will only be one key. I believe this is guaranteed by Smithy.

@kuhe kuhe added the pr/needs-review This PR needs a review from a Member. label Oct 28, 2022
@trivikr trivikr changed the title fix(types): Corrected extends check of generic in WithSdkStreamMixin fix(types): extends check of generic in WithSdkStreamMixin Oct 29, 2022
@kuhe kuhe merged commit 299d245 into aws:main Oct 31, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr/needs-review This PR needs a review from a Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants