diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index e170514fd..a4cb119c0 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -257,7 +257,7 @@ class TsGenerator : public BaseGenerator { for (const auto &it : ns_defs_) { code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n" + "/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */\n\n"; - + // export all definitions in ns entry point module int export_counter = 0; for (const auto &def : it.second.definitions) { @@ -567,7 +567,7 @@ class TsGenerator : public BaseGenerator { static Type GetUnionUnderlyingType(const Type &type) { - if (type.enum_def != nullptr && + if (type.enum_def != nullptr && type.enum_def->underlying_type.base_type != type.base_type) { return type.enum_def->underlying_type; } else { @@ -1849,7 +1849,7 @@ class TsGenerator : public BaseGenerator { code += "BigInt(0)"; } else if (IsScalar(field.value.type.element)) { if (field.value.type.enum_def) { - code += field.value.constant; + code += "null"; } else { code += "0"; } diff --git a/tests/ts/my-game/example/monster.js b/tests/ts/my-game/example/monster.js index 11a6604b2..2bbcb9ff1 100644 --- a/tests/ts/my-game/example/monster.js +++ b/tests/ts/my-game/example/monster.js @@ -457,7 +457,7 @@ export class Monster { } vectorOfEnums(index) { const offset = this.bb.__offset(this.bb_pos, 98); - return offset ? this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index) : 0; + return offset ? this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index) : null; } vectorOfEnumsLength() { const offset = this.bb.__offset(this.bb_pos, 98); diff --git a/tests/ts/my-game/example/monster.ts b/tests/ts/my-game/example/monster.ts index c94e30614..289f609a8 100644 --- a/tests/ts/my-game/example/monster.ts +++ b/tests/ts/my-game/example/monster.ts @@ -594,7 +594,7 @@ anyAmbiguous(obj:any):any|null { vectorOfEnums(index: number):Color|null { const offset = this.bb!.__offset(this.bb_pos, 98); - return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : 0; + return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : null; } vectorOfEnumsLength():number { diff --git a/tests/ts/union-underlying-type/d.js b/tests/ts/union-underlying-type/d.js index da66e15c0..d85262ec8 100644 --- a/tests/ts/union-underlying-type/d.js +++ b/tests/ts/union-underlying-type/d.js @@ -29,7 +29,7 @@ export class D { } testVectorOfUnionType(index) { const offset = this.bb.__offset(this.bb_pos, 8); - return offset ? this.bb.readInt32(this.bb.__vector(this.bb_pos + offset) + index * 4) : 0; + return offset ? this.bb.readInt32(this.bb.__vector(this.bb_pos + offset) + index * 4) : null; } testVectorOfUnionTypeLength() { const offset = this.bb.__offset(this.bb_pos, 8); diff --git a/tests/ts/union-underlying-type/d.ts b/tests/ts/union-underlying-type/d.ts index c90e167d1..b462febe6 100644 --- a/tests/ts/union-underlying-type/d.ts +++ b/tests/ts/union-underlying-type/d.ts @@ -40,7 +40,7 @@ testUnion(obj:any):any|null { testVectorOfUnionType(index: number):ABC|null { const offset = this.bb!.__offset(this.bb_pos, 8); - return offset ? this.bb!.readInt32(this.bb!.__vector(this.bb_pos + offset) + index * 4) : 0; + return offset ? this.bb!.readInt32(this.bb!.__vector(this.bb_pos + offset) + index * 4) : null; } testVectorOfUnionTypeLength():number { diff --git a/tests/ts/union_vector/movie.js b/tests/ts/union_vector/movie.js index da5d1fada..81dee0e67 100644 --- a/tests/ts/union_vector/movie.js +++ b/tests/ts/union_vector/movie.js @@ -32,7 +32,7 @@ export class Movie { } charactersType(index) { const offset = this.bb.__offset(this.bb_pos, 8); - return offset ? this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index) : 0; + return offset ? this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index) : null; } charactersTypeLength() { const offset = this.bb.__offset(this.bb_pos, 8); diff --git a/tests/ts/union_vector/movie.ts b/tests/ts/union_vector/movie.ts index 0e2370a89..f3457fb43 100644 --- a/tests/ts/union_vector/movie.ts +++ b/tests/ts/union_vector/movie.ts @@ -44,7 +44,7 @@ mainCharacter(obj:any|string):any|string|null { charactersType(index: number):Character|null { const offset = this.bb!.__offset(this.bb_pos, 8); - return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : 0; + return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : null; } charactersTypeLength():number {