Skip to content

Conversation

@ibc
Copy link

@ibc ibc commented Dec 17, 2025

Details

# Details

- Fixes google#7656
- Added a new `--ts-undefined-for-optionals` command line option for `flatc`.
- If enabled, generated TypeScript code uses `undefined` for optional fields rather than `null`.
@ibc
Copy link
Author

ibc commented Dec 17, 2025

Note for myself: run sh scripts/clang-format-git.sh after C++ changes.

@ibc
Copy link
Author

ibc commented Dec 18, 2025

This is ready.

@ibc ibc marked this pull request as ready for review December 18, 2025 12:01
@bjornharrtell bjornharrtell self-requested a review December 18, 2025 12:05
bjornharrtell
bjornharrtell previously approved these changes Dec 18, 2025
Copy link
Collaborator

@bjornharrtell bjornharrtell left a comment

Choose a reason for hiding this comment

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

Some perhaps unnecessary source format changes but otherwise looks like solid work

@ibc
Copy link
Author

ibc commented Dec 18, 2025

Some perhaps unnecessary source format changes but otherwise looks like solid work

Could you detail which unnecessary source format changes you mean? I've run sh scripts/clang-format-git.sh as documented and on TS and Python files I've followed existing syntax.

@bjornharrtell
Copy link
Collaborator

@aardappel I think it's is good to go but can't merge due to the test failure for swift which I would think is unrelated to this PR.

@jmillan
Copy link
Contributor

jmillan commented Dec 18, 2025

@aardappel I think it's is good to go but can't merge due to the test failure for swift which I would think is unrelated to this PR.

The error is due to a HTTP download timeout. Probably fixable by re-running the GH action.

@ibc
Copy link
Author

ibc commented Dec 18, 2025

@aardappel I think it's is good to go but can't merge due to the test failure for swift which I would think is unrelated to this PR.

It seems a network issue. Maybe re-running the job could work? Note: I cannot rerun it.

@bjornharrtell
Copy link
Collaborator

@aardappel I was able to trigger re-run and it seems to work now.. but can't because of this:

image

No idea what that means.

@aardappel
Copy link
Collaborator

afaik @dbaileychess has locked the repo for some to changes how things are set up.. we'll have to wait till he is done before further PRs can get merged.

@dbaileychess
Copy link
Collaborator

I'm trying to minimize changes while I am working on merging the last 6 months of changes back into Google. Hopefully I can finish tonight or tomorrow, can this wait?

"throw", "true", "try", "typeof", "var",
"void", "while", "with", "as", "implements",
"interface", "let", "package", "private", "protected",
"public", "static", "yield", "undefined"};
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add a trailing comment to it formats this nicely?

Copy link
Author

Choose a reason for hiding this comment

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

Done.


// Value for non present optional fields. It's "null" by default and
// "undefined" if parser.ts_undefined_for_optionals flag is set.
std::string null_ = "null";
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you name this better?

Copy link
Author

Choose a reason for hiding this comment

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

I've renamed it to null_keyword_.

std::string GenDefaultValue(const FieldDef& field, import_set& imports) {
if (field.IsScalarOptional()) {
return "null";
return null_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

are all "null" supposed to be replaced? Or just for optional stuff.

Copy link
Author

Choose a reason for hiding this comment

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

The purpose of the new flag is to produce TS code with value undefined rather than null when the key has no value. This includes the default value.

For example, if the FBS schema is like this:

table ScalarStuff {
  just_i8: int8;
  maybe_i8: int8 = null;
}

Then, if --ts-undefined-for-optionals is set and no explicit value is given to maybe_i8, its default value in TS will be undefined rather than null. This behavior is consistent with the intended behavior.

@ibc ibc requested a review from aardappel as a code owner December 19, 2025 11:11
@ibc
Copy link
Author

ibc commented Dec 19, 2025

I'm trying to minimize changes while I am working on merging the last 6 months of changes back into Google. Hopefully I can finish tonight or tomorrow, can this wait?

Sure, no hurries. BTW I've addressed feedback already.
Thanks a lot.

@jtdavis777 jtdavis777 added the ready-for-merge This PR has been approved by a maintainer and is ready for merge by a code owner label Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema javascript python ready-for-merge This PR has been approved by a maintainer and is ready for merge by a code owner typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TS] Feature Request: Make optional field getters return undefined (instead of null) if the value is not set

6 participants