Skip to content

[ts] erroneous mutable API generated for boolean fields in structs #8676

Closed

Description

@nurbo

I came across a missing boolean/number conversion issue when compiling files with generated mutable APIs (--gen-mutable flag).

Given the following schema:

struct s {
  flag: bool;
}

table t {
  flag: bool;
}

Compiled with flatc (version 25.2.10):

> flatc --ts --gen-mutable -o ./tmp tmp.fbs

The mutable API for struct s will have the following:

mutate_flag(value:boolean):boolean {
  this.bb!.writeInt8(this.bb_pos + 0, value);
  return true;
}

Which causes an error with TypeScript 5.9.2:

error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number'.
21   this.bb!.writeInt8(this.bb_pos + 0, value);
                                         ~~~~~

Equivalent generated API for table t does have the correct boolean-to-number conversion (using preceding +):

mutate_flag(value:boolean):boolean {
  const offset = this.bb!.__offset(this.bb_pos, 4);

  if (offset === 0) {
    return false;
  }

  this.bb!.writeInt8(this.bb_pos + offset, +value);
  return true;
}

Activity

  1. nurbo commented on Aug 18, 2025

    @nurbo
    ContributorAuthor

    Looks like this special case:

    flatbuffers/src/idl_gen_ts.cpp

    Lines 1907 to 1909 in 067bfdb

    code +=
    " " + GenBBAccess() + write_method + "(this.bb_pos + offset, ";
    if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }

    should have been also applied the statement above:

    flatbuffers/src/idl_gen_ts.cpp

    Lines 1895 to 1898 in 067bfdb

    if (struct_def.fixed) {
    code += " " + GenBBAccess() + write_method + "(this.bb_pos + " +
    NumToString(field.value.offset) + ", ";
    } else {

    Or rather outside of the conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions