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

Jsonify: handle undefined in array. #310

Merged

Conversation

darcyparker
Copy link
Contributor

@darcyparker darcyparker commented Nov 4, 2021

Fixes #309

@bbrk24 - Please review and suggest tests if you see anything I missed.

I did not address Jsonify<{ x?: string }>. This seems to be a harder problem.

I experimented with

type RequiredKeys<T> = {
	[K in keyof T]-?: (
		{} extends {[P in K]: T[K]} ? // {} extends optional, but not required
		never :
		K
	)
}[keyof T];

type OptionalKeys<T> = {
	[K in keyof T]-?: (
		{} extends {[P in K]: T[K]} ? // {} extends optional, but not required
		K :
		never
	)
}[keyof T];

type JsonifyObject<T extends object> =
	{[P in RequiredKeys<T>]: Jsonify<T[P]>} &
	{[P in OptionalKeys<T>]?: Jsonify<Exclude<T[P], undefined>>;};

And adding JsonifyObject<T to Jsonify<T>...

But as I started to write tests, I realized I have an issue with JsonObject's implementation

// Special cases of Object with optional member
declare const objectWithUndefinedValue: {x: undefined};
declare const parsedStringifiedObjectWithUndefinedValue: Jsonify<{x: undefined}>;
expectNotAssignable<JsonValue>(objectWithUndefinedValue); //I expected this to pass
expectAssignable<JsonValue>(parsedStringifiedObjectWithUndefinedValue);
expectAssignable<{}>(parsedStringifiedObjectWithUndefinedValue);

declare const objectWithOptionalValue: {x?: string};
expectAssignable<JsonValue>(objectWithOptionalValue);

I feel #65 had the right idea and should allow optional key-value pairs, but not a key-value pair where the value is undefined.

I need to come back to this... but wanted to leave the above notes if someone else wants to take a crack at it.

@darcyparker darcyparker marked this pull request as draft November 4, 2021 16:13
Copy link
Contributor

@bbrk24 bbrk24 left a comment

Choose a reason for hiding this comment

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

One minor suggestion for the test, other than that everything looks good.

Comment on lines 130 to 131
declare const parsedStringifiedArrayWithUndefined1: Jsonify<[undefined]>; // = JSON.parse(JSON.stringify([undefined]));
expectType<null[]>(parsedStringifiedArrayWithUndefined1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it's expecting null[], I think it should test Jsonify<undefined[]> for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure what you mean.

I am verifying Jsonify<undefined[]> is the expected null[].

expectType<Jsonify<undefined[]>(parsedStringifiedArrayWithUndefined1); would be true if the implementation of Jsonify was wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am verifying Jsonify<undefined[]> is the expected null[].

I am saying that it should do that. However, it currently doesn't test Jsonify<undefined[]>, it tests Jsonify<[undefined]>.

Ultimately it isn't a big deal, I just didn't see any point in using a tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah - I get what you're saying now. Thanks. I will make that correction when I get back to this problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 68474d1

@sindresorhus
Copy link
Owner

Note that #312 was merged.

@sindresorhus
Copy link
Owner

It's unclear to me whether this is still needed after #312?

@darcyparker
Copy link
Contributor Author

I think this is still relevant. It handles undefined in array correctly. See #309 where it was noted that "undefined in an array becomes null". I am sorry this sat in draft. #312 does solve the other part of #309.

https://github.com/sindresorhus/type-fest/pull/310/files#r743986229 has a small comment about the tests. I tested [undefined] and not the more general undefined[].

@darcyparker darcyparker marked this pull request as ready for review March 22, 2022 12:05
@darcyparker
Copy link
Contributor Author

@sindresorhus - Updated and ready for review.

@sindresorhus sindresorhus merged commit 17b0235 into sindresorhus:main Mar 30, 2022
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.

Jsonify removes all optional properties
3 participants