Skip to content

Allow optional fields to be explicitly set to undefined#374

Closed
haines wants to merge 1 commit intobufbuild:mainfrom
haines:exact-optional-property-types
Closed

Allow optional fields to be explicitly set to undefined#374
haines wants to merge 1 commit intobufbuild:mainfrom
haines:exact-optional-property-types

Conversation

@haines
Copy link
Contributor

@haines haines commented Jan 27, 2023

This PR allows fields to be set to undefined explicitly even when the TypeScript compiler option exactOptionalPropertyTypes is enabled.

Currently optional message fields are typed as

field?: Type

but with exactOptionalPropertyTypes this means you can't set them to undefined; you have to omit them. This is a bit inconvenient if you're trying to pass through a value from a variable.

This PR changes the optional fields to be typed as

field?: Type | undefined

which removes the restriction.

@smaye81
Copy link
Contributor

smaye81 commented Feb 7, 2023

Hey @haines thank you for the PR. Appreciate you being patient.

Our approach has been that we support the default compiler settings for a particular version of TypeScript. And since exactOptionalPropertyTypes: true is not a default setting, I don't think it's something we want to factor into the generated code. It's just a can of worms we are trying to avoid because trying to handle the myriad options in tsconfig would be a difficult task.

That being said, if this becomes a default setting, we would certainly support it at that time. Thanks again for the consideration.

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.

2 participants