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

[email protected]: Empty object check error #4632

Closed
zhe-he opened this issue Dec 5, 2023 · 4 comments
Closed

[email protected]: Empty object check error #4632

zhe-he opened this issue Dec 5, 2023 · 4 comments

Comments

@zhe-he
Copy link
Contributor

zhe-he commented Dec 5, 2023

https://github.com/reduxjs/redux/blob/0e8a7b00db58796f7a7adef6ac9ebed9420c18cb/src/utils/isPlainObject.ts#L5C1-L14C2
image

I just upgraded the package to @reduxjs/[email protected] and [email protected] today, and then I found that the console reported an error, A non-serializable value was detected in the state, in the path. Finally, I found that it was because the object I created through Object.create(null) did not pass the isPlainObject function check. I want to know if this is correct, um... Do not want users to use objects created by Object.create(null) or is this function check not comprehensive enough?

console.log( {} ) // true
console.log( isPlainObject(Object.create(null)) ) // false ???
@markerikson
Copy link
Contributor

Out of curiosity, what's the use case for Object.create(null) here? What are you doing with it in code?

@zhe-he
Copy link
Contributor Author

zhe-he commented Dec 5, 2023

I use it just to create an empty object for storing data, and later I will add keys and values to this empty object. In some cases, it just makes it convenient for me to have fewer declarations, for example, declaring AnyObject explicitly.

const data = Object.create(null);
data.a = 1;

const data: AnyObject = {};
data.a = 1;

@markerikson
Copy link
Contributor

@zhe-he : I'm okay with merging the fix, but tbh I'm not sure I see an actual reason to use Object.create(null) here :) the AnyObject example is actually shorter, and also more clear about what's going on.

@zhe-he
Copy link
Contributor Author

zhe-he commented Dec 5, 2023

Well..., I'm not trying to convince you which method is better, I think any method is fine, it's just based on my personal habits.
I don't like explicit declarations, so I used the any returned by Object.create. At the same time, I personally think that the Object.create(null) loop will be a bit faster, so I used this, below is the time spent on testing.

image
// 2 lines
import { AnyObject } from "@/@types";
const data: AnyObject = {};

// 1 line
const data = Object.create(null);


// 2 lines or 9 lines
export type M = {
    name: string;
    value: string;
    child?: M[];
}
export type O = {
    [props: string]: M
}
// import { O } from "@/@types";
const map: O = {}

// 1 line
const map = Object.create(null);

@timdorr timdorr closed this as completed Dec 6, 2023
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

No branches or pull requests

3 participants