Skip to content

fix(swc): don't set missing class fields#2155

Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom
rickdgeerling:patch-1
Jul 11, 2023
Merged

fix(swc): don't set missing class fields#2155
kamilmysliwiec merged 1 commit intonestjs:masterfrom
rickdgeerling:patch-1

Conversation

@rickdgeerling
Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

SWC adopts ESNext behaviour of explicitly setting fields as undefined by default. Disabling this behaviour aligns better with TS that targets ES2021.

The result is as following:

import { plainToInstance } from "class-transformer";

class SomeClass {
  public aNumber: number;
  public aString: string;
}

const partialA = plainToInstance(SomeClass, { aNumber: 1 });
const partialB = plainToInstance(SomeClass, { aString: "abc" });
console.log({ ...partialA, ...partialB });
// Expected: { aNumber: 1, aString: 'abc' }
// Actual:   { aNumber: undefined, aString: 'abc' }

Issue Number: N/A

What is the new behavior?

console.log({ ...partialA, ...partialB });
// Logs: { aNumber: 1, aString: 'abc' }

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Relates to swc-project/swc#3459

Reproduction repo available https://github.com/tuxmachine/swc-class-transformer

SWC adopts ESNext behaviour of explicitly setting fields as `undefined` by default. Disabling this behaviour aligns better with TS that targets ES2021
@kamilmysliwiec
Copy link
Copy Markdown
Member

LGTM ❤️

@kamilmysliwiec kamilmysliwiec merged commit be5d1d0 into nestjs:master Jul 11, 2023
@rickdgeerling rickdgeerling deleted the patch-1 branch July 11, 2023 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants