Skip to content
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: exclude class instance from DeepSignal #4605

Conversation

rainerhahnekamp
Copy link
Contributor

This is a draft. Whoever has an idea, for the fix, please let me know. It tries to solve the following problem.

class User {
  id = 0;
  name = 'Konrad';
}

const state = signalState({ user: new User() });
const user = state.user; // should not be DeepSignal<User>

IsRecord tries to differentiate a class instance from object literal by checking
if a prototype is there or not.

Fore more details, checkout the commit.

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
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #4604

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[ ] No

This is a draft. `IsRecord` tries to differentiate
a class instance from object literal by checking
if a prototype is there or not.

It kind of works but breaks a lot of other
functions depending on it.
Copy link

netlify bot commented Nov 22, 2024

Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 5d2572e
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/67411677a6326400080901bd
😎 Deploy Preview https://deploy-preview-4605--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@jdegand
Copy link

jdegand commented Nov 26, 2024

I assume you used constructor: Function and that didn't work.

Maybe, check if the constructor.name is 'Object' (true for object literals but not for class instances)?

export type IsRecord<T> = T extends object
  ? T extends unknown[]
    ? false
    : T extends Set<unknown>
    ? false
    : T extends Map<unknown, unknown>
    ? false
    : T extends Function
    ? false
    : T extends { prototype: unknown } // Checks for class instances
    ? false
    : T extends { constructor: { name: string } }
    ? T['constructor']['name'] extends 'Object'
      ? true
      : false
    : false
  : false;

@rainerhahnekamp
Copy link
Contributor Author

@jdegand: Nope, in the meantime, I've pulled out a TypeScript Playground, which allows us to experiment a little bit.

I am going to share the Link in the original issue, and I'm going to close this PR since it is not ready yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

signalState is not creating DeepSignal property signals when the value object is an instance of a class
2 participants