diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index f61f875d7..dedbec033 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -505,8 +505,12 @@ class TsGenerator : public BaseGenerator { std::string enum_name = AddImport(imports, *value.type.enum_def, *value.type.enum_def) .name; - std::string enum_value = namer_.Variant( - *value.type.enum_def->FindByValue(value.constant)); + const EnumVal* val = + value.type.enum_def->FindByValue(value.constant); + if (val == nullptr) { + val = value.type.enum_def->MinValue(); + } + std::string enum_value = namer_.Variant(*val); ret += enum_name + "." + enum_value + (i < value.type.fixed_length - 1 ? ", " : ""); } @@ -521,9 +525,10 @@ class TsGenerator : public BaseGenerator { return "BigInt('" + value.constant + "')"; } default: { - EnumVal* val = value.type.enum_def->FindByValue(value.constant); - if (val == nullptr) - val = const_cast(value.type.enum_def->MinValue()); + const EnumVal* val = value.type.enum_def->FindByValue(value.constant); + if (val == nullptr) { + val = value.type.enum_def->MinValue(); + } return AddImport(imports, *value.type.enum_def, *value.type.enum_def) .name + "." + namer_.Variant(*val); diff --git a/tests/non_zero_enum.fbs b/tests/non_zero_enum.fbs new file mode 100644 index 000000000..1e1574a7b --- /dev/null +++ b/tests/non_zero_enum.fbs @@ -0,0 +1,12 @@ +enum NonZero: ubyte { + VAL = 1, +} + +struct NonZeroArrayStruct { + data: [NonZero:4]; +} + +table NonZeroVectorTable { + values: [NonZero]; + value: NonZero = VAL; +} diff --git a/tests/ts/TypeScriptTest.py b/tests/ts/TypeScriptTest.py index aba6ffc3c..a12bc25ab 100755 --- a/tests/ts/TypeScriptTest.py +++ b/tests/ts/TypeScriptTest.py @@ -94,6 +94,11 @@ flatc( data="../unicode_test.json", ) +flatc( + options=["--ts", "--gen-object-api"], + schema="../non_zero_enum.fbs", +) + flatc( options=[ "--ts",