Files
flatbuffers-bigfoot/docs/source/TypeScriptUsage.md
Björn Harrtell 760c657551 [TS/JS] New gen TS code gen (#6302)
* TS/ES6 modules spike iteration 1

* Initial modularized dasherized output

* Remove obsoleted parts and namespace wrapping

* Use _flatbuffers_ prefix

* First part of imports logic

* Second part of imports logic

* Fix TS/JS code removal mixup

* Alias imported symbols if same name from different namespaces and some fixes

* Use star import for bare imports

* Fix messed up string concat

* var to const and remove not needed semi

* Remove some cases of ns prefixing

* Add missing space

* Cleanups

* Completed initial import tracking logic

* Compilable output

* Adjust TypeScriptTest and dependents to work

* Use local flatbuffers package for tests

* Refactor away use of any

* Remove obsolete imported_fileset and reexport_map

* Still need any and fix JavaScriptTest.sh

* Fix test runs out of the box

* Temp add generated files

* TypeScriptTest replaces JavaScriptTest and cleanups

* Also remove reference to JavaScriptTest in TestAll.sh

* Remove old generated ts/js files

* Remove use of --js in generate_code scripts

* idl_gen_js_ts to idl_gen_ts and removal of js gen

* Remove obsoleted options

* Fix obsolete ts test detection

* Tweak ts compilation be as strict as possible

* Remove jsdoc type annotation generation

* Generated test ts files

* Fix search and replace messup

* Regenerated ts test output

* Use CharToLower

* Use normal for loop

* Rework namespacedir

* Revert "Rework namespacedir"

This reverts commit 6f4eb0104ceeb86011bb076ebca901138c48e068.

* Revert "Use normal for loop"

This reverts commit 676b2135bfaa1853dfbb06c92b5c16a0d81bb13a.

* Revert "Use CharToLower"

This reverts commit 2d08648d0d72d0af201fad80d54cdc76412b35e9.

* Again do rework but correct

* Avoid runtime cast

* Fix test runs

* Also add npm install to get tsc

* Bump node test versions

* for range to std for loop

* Clang format

* Missed one clang format

* Move accessor to later

* Attempt to make windows version of TypeScriptTest

* Want to see the output

* Try to get newer node at appveyor

* Style changes
2021-01-19 12:51:13 -08:00

3.5 KiB

Use in TypeScript

Before you get started

Before diving into the FlatBuffers usage in TypeScript, it should be noted that the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general FlatBuffers usage in all of the supported languages (including TypeScript). This page is specifically designed to cover the nuances of FlatBuffers usage in TypeScript.

You should also have read the [Building](@ref flatbuffers_guide_building) documentation to build flatc and should be familiar with [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and [Writing a schema](@ref flatbuffers_guide_writing_schema).

FlatBuffers TypeScript library code location

The code for the FlatBuffers TypeScript library can be found at flatbuffers/js with typings available at @types/flatbuffers.

Testing the FlatBuffers TypeScript library

To run the tests, use the [TypeScriptTest.sh](https://github.com/google/ flatbuffers/blob/master/tests/TypeScriptTest.sh) shell script.

Note: The TypeScript test file requires Node.js.

Using the FlatBuffers TypeScript libary

Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth example of how to use FlatBuffers in TypeScript.

FlatBuffers supports both reading and writing FlatBuffers in TypeScript.

To use FlatBuffers in your own code, first generate TypeScript classes from your schema with the --ts option to flatc. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.

For example, here is how you would read a FlatBuffer binary file in TypeScript: First, include the library and generated code. Then read the file into an Uint8Array. Make a flatbuffers.ByteBuffer out of the Uint8Array, and pass the ByteBuffer to the getRootAsMonster function.

  // note: import flatbuffers with your desired import method

  import { MyGame } from './monster_generated';

  let data = new Uint8Array(fs.readFileSync('monster.dat'));
  let buf = new flatbuffers.ByteBuffer(data);

  let monster = MyGame.Example.Monster.getRootAsMonster(buf);

Now you can access values like this:

  let hp = monster.hp();
  let pos = monster.pos();

Object based API

FlatBuffers is all about memory efficiency, which is why its base API is written around using as little as possible of it. This does make the API clumsier (requiring pre-order construction of all data, and making mutation harder).

For times when efficiency is less important a more convenient object based API can be used (through --gen-object-api) that is able to unpack & pack a FlatBuffer into objects and standard TS types.

To use:

    // Autogenerated class from table Monster.
    let monsterobj = new MonsterT();

    // Deserialize from buffer into object.
    Monster.getRootAsMonster(flatbuffer).unpackTo(monsterobj);
    // or
    let monsterobj = Monster.getRootAsMonster(flatbuffer).unpack();

    // Update object directly like a regular TS class instance.
    console.log(monsterobj.name);
    monsterobj.name = "Bob";

    // Serialize into new flatbuffer.
    let fbb = new flatbuffers.Builder(1);
    Monster.finishMonsterBuffer(fbb, monsterobj.pack(fbb));

Text parsing FlatBuffers in TypeScript

There currently is no support for parsing text (Schema's and JSON) directly from TypeScript.