Skip to content

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

@nurbo

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;
}

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