Skip to content

Jsonify: handle unknown as JsonValue#1206

Merged
sindresorhus merged 3 commits intosindresorhus:mainfrom
GerkinDev:fix/jsonify/support-unknown
Aug 24, 2025
Merged

Jsonify: handle unknown as JsonValue#1206
sindresorhus merged 3 commits intosindresorhus:mainfrom
GerkinDev:fix/jsonify/support-unknown

Conversation

@GerkinDev
Copy link
Copy Markdown
Contributor

@GerkinDev GerkinDev commented Jul 28, 2025

Closes #1195 by mapping unknown to JsonValue. If requested, I can preserve unknown as mentioned in the issue

Had a bit of struggles with the test

declare const tupleStringJson: Jsonify<string[] & ['some value']>;
expectType<['some value']>(tupleStringJson);

because

type JsonifyList<T extends UnknownArray> = T extends readonly [infer F, ...infer R]

infered {F: 'some value', R: unknown[]}, thus the arguably not ideal hack. Yet, it passes all the tests and it somewhat makes sense.

This problem looks like a known issue in TS tuples handling & intersection with arrays, but depending on the POV, it may be more of a feature than a bug.

[Key in keyof T]: IsAny<T[Key]> extends true
? Key
: undefined extends T[Key]
: IsUnknown<T[Key]> extends true ? Key : undefined extends T[Key]
Copy link
Copy Markdown
Contributor Author

@GerkinDev GerkinDev Jul 28, 2025

Choose a reason for hiding this comment

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

Given how any is handled I suppose it makes sense

@sindresorhus
Copy link
Copy Markdown
Owner

If requested, I can preserve unknown as mentioned in the issue

Let’s not preserve unknown. Jsonify’s job is to produce something assignable to JsonValue, so mapping unknown to JsonValue keeps the contract clear and makes it more
ergonomic without sacrificing soundness.

@sindresorhus sindresorhus merged commit 642bb13 into sindresorhus:main Aug 24, 2025
6 checks passed
@GerkinDev GerkinDev deleted the fix/jsonify/support-unknown branch September 5, 2025 15:19
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<unknown> should be either unknown or JsonValue

2 participants