Skip to content

Support exactOptionalPropertyTypes#1371

Merged
timostamm merged 5 commits into
bufbuild:mainfrom
haines:exact-optional-property-types
Apr 20, 2026
Merged

Support exactOptionalPropertyTypes#1371
timostamm merged 5 commits into
bufbuild:mainfrom
haines:exact-optional-property-types

Conversation

@haines

@haines haines commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Closes #1358

This PR adds support for the exactOptionalPropertyTypes TypeScript compiler option, which is on by default in tsc --init from Typescript 5.9 onwards. This necessitates adding | undefined to the typing of optional fields so that they may be explicitly set to undefined, not just omitted.

I've enabled the option in the compiler settings for this project to catch any issues, which meant adding a few | undefineds to types used internally as well.

I didn't add it to the JSON types because doing so means either adding | undefined to the fields of JsonObject (which seems wrong when deserializing from JSON can never produce an undefined field) or removing the constraint that MessageJsonType extends JsonValue (which seems wrong because the JSON types are JSON values).

@timostamm

Copy link
Copy Markdown
Member

Thanks for the PR, Andrew! I'm a bit distracted by other things right now, but I'll check it out soon.

@timostamm timostamm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR, Andrew 🙂

I was afraid this would entail more changes in our runtime packages, but this all looks reasonable to me. I did push up a small change to toJson and fromJson, and updated the code snippets in the manual to match the new generated code.

I didn't add it to the JSON types

Agreed.

@timostamm timostamm merged commit 6be4408 into bufbuild:main Apr 20, 2026
25 checks passed
@Zaczero

Zaczero commented Apr 20, 2026

Copy link
Copy Markdown

I don't want to sound salty. But... my original PR #1347 was closed incorrectly, then you made me open an issue to reopen that PR, and instead you just resolved the issue yourself without giving a chance to contribute. And not even co-authored-by.

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.

Investigate support for exactOptionalPropertyTypes (on by default from TypeScript 5.9)

3 participants