diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index edcb780f0..9fac55038 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -955,7 +955,7 @@ void GenStruct(const Parser &parser, StructDef &struct_def, if (lang_.language == IDLOptions::kTs) { code += "static create" + struct_def.name + "(builder:flatbuffers.Builder"; - code += arguments + "):flatbuffers.Offset|null {\n"; + code += arguments + "):flatbuffers.Offset {\n"; } else { code += object_name + ".create" + struct_def.name + " = function(builder"; code += arguments + ") {\n"; @@ -1049,8 +1049,7 @@ void GenStruct(const Parser &parser, StructDef &struct_def, type += " | Uint8Array"; } code += "Vector(builder:flatbuffers.Builder, data:" + type + - "):flatbuffers.Offset|null {\n"; - code += "if(!data){\n return null;\n}\n"; + "):flatbuffers.Offset {\n"; } else { code += object_name + ".create" + MakeCamel(field.name); code += "Vector = function(builder, data) {\n"; diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index b91ff4db6..78871d504 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/monster_test_generated.ts b/tests/monster_test_generated.ts index 67206a544..aeee6fc90 100644 --- a/tests/monster_test_generated.ts +++ b/tests/monster_test_generated.ts @@ -148,7 +148,7 @@ mutate_b(value:number):boolean { * @param {number} b * @returns {flatbuffers.Offset} */ -static createTest(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset|null { +static createTest(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset { builder.prep(2, 4); builder.pad(1); builder.writeInt8(b); @@ -395,7 +395,7 @@ test3(obj?:MyGame.Example.Test):MyGame.Example.Test|null { * @param {number} test3_b * @returns {flatbuffers.Offset} */ -static createVec3(builder:flatbuffers.Builder, x: number, y: number, z: number, test1: number, test2: MyGame.Example.Color, test3_a: number, test3_b: number):flatbuffers.Offset|null { +static createVec3(builder:flatbuffers.Builder, x: number, y: number, z: number, test1: number, test2: MyGame.Example.Color, test3_a: number, test3_b: number):flatbuffers.Offset { builder.prep(16, 32); builder.pad(2); builder.prep(2, 4); @@ -489,7 +489,7 @@ mutate_distance(value:number):boolean { * @param {number} distance * @returns {flatbuffers.Offset} */ -static createAbility(builder:flatbuffers.Builder, id: number, distance: number):flatbuffers.Offset|null { +static createAbility(builder:flatbuffers.Builder, id: number, distance: number):flatbuffers.Offset { builder.prep(4, 8); builder.writeInt32(distance); builder.writeInt32(id); @@ -1314,10 +1314,7 @@ static addInventory(builder:flatbuffers.Builder, inventoryOffset:flatbuffers.Off * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createInventoryVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createInventoryVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(data[i]); @@ -1386,10 +1383,7 @@ static addTestarrayofstring(builder:flatbuffers.Builder, testarrayofstringOffset * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofstringVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofstringVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); @@ -1418,10 +1412,7 @@ static addTestarrayoftables(builder:flatbuffers.Builder, testarrayoftablesOffset * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayoftablesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayoftablesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); @@ -1458,10 +1449,7 @@ static addTestnestedflatbuffer(builder:flatbuffers.Builder, testnestedflatbuffer * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestnestedflatbufferVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestnestedflatbufferVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(data[i]); @@ -1570,10 +1558,7 @@ static addTestarrayofbools(builder:flatbuffers.Builder, testarrayofboolsOffset:f * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofboolsVector(builder:flatbuffers.Builder, data:boolean[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofboolsVector(builder:flatbuffers.Builder, data:boolean[]):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(+data[i]); @@ -1626,10 +1611,7 @@ static addTestarrayofstring2(builder:flatbuffers.Builder, testarrayofstring2Offs * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofstring2Vector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofstring2Vector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); diff --git a/tests/namespace_test/namespace_test1_generated.ts b/tests/namespace_test/namespace_test1_generated.ts index 59b6f5aee..54d935a4e 100644 --- a/tests/namespace_test/namespace_test1_generated.ts +++ b/tests/namespace_test/namespace_test1_generated.ts @@ -168,7 +168,7 @@ mutate_b(value:number):boolean { * @param {number} b * @returns {flatbuffers.Offset} */ -static createStructInNestedNS(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset|null { +static createStructInNestedNS(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset { builder.prep(4, 8); builder.writeInt32(b); builder.writeInt32(a);