Skip to content

Jsonify: Fix optional properties leaking undefined into the key set - #1484

Merged
sindresorhus merged 3 commits into
sindresorhus:mainfrom
Linell:fix-jsonify-optional-keys
Jul 13, 2026
Merged

sindresorhus merged 3 commits into
sindresorhus:mainfrom
Linell:fix-jsonify-optional-keys

Conversation

@Linell

@Linell Linell commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

FilterJsonableKeys maps each property to its own name and reads all values back out with an indexed access. Reading an optional property adds undefined to the result, so for {a: string; b?: string} the key union became "a" | "b" | undefined. Feeding that union to Pick silently builds an object whose key set includes undefined, and re-applying Jsonify to that output walks the corrupted key set, computes the illegal lookup T[undefined], and collapses the object to {}.

Excluding undefined from the union keeps the key set clean and makes Jsonify<Jsonify<T>> equal Jsonify<T>.

Fixes discovered downstream in inngest/inngest#4483.

`FilterJsonableKeys` maps each property to its own name and reads all
values back out with an indexed access. Reading an optional property adds
`undefined` to the result, so for `{a: string; b?: string}` the key union
became `"a" | "b" | undefined`. Feeding that union to `Pick` silently
builds an object whose key set includes `undefined`, and re-applying
`Jsonify` to that output walks the corrupted key set, computes the
illegal lookup `T[undefined]`, and collapses the object to `{}`.

Excluding `undefined` from the union keeps the key set clean and makes
`Jsonify<Jsonify<T>>` equal `Jsonify<T>`.

Fixes discovered downstream in inngest/inngest#4483.
Comment thread source/jsonify.d.ts
Comment on lines 36 to 38
type JsonifyObject<T extends object> = {
[Key in keyof Pick<T, FilterJsonableKeys<T>>]: Jsonify<T[Key]>;
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

FilterJsonableKeys is actually not required at all, that's just unnecessary code.

Updating JsonifyObject to the following is a better fix:

Suggested change
type JsonifyObject<T extends object> = {
[Key in keyof T as T[Key] extends NotJsonable ? never : Key]: Jsonify<T[Key]>;
};

This passes all the test cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ohhh you're right, updated! 👍 Thank you!

@Linell
Linell requested a review from som-sm July 13, 2026 14:35
@sindresorhus
sindresorhus merged commit 5339fb3 into sindresorhus:main Jul 13, 2026
11 checks passed
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.

3 participants