mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-20 12:55:07 +00:00
[TS] Make strict compliant and improve typings (#7549)
* [TS] Make strict compliant and improve typings * clang-format * Code gen harmonize Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -549,7 +549,7 @@ export class Builder {
|
||||
*
|
||||
* @returns offset of obj
|
||||
*/
|
||||
createObjectOffset(obj: string | any): Offset {
|
||||
createObjectOffset(obj: string | IGeneratedObject | null): Offset {
|
||||
if(obj === null) {
|
||||
return 0
|
||||
}
|
||||
@@ -566,7 +566,7 @@ export class Builder {
|
||||
*
|
||||
* @returns list of offsets of each non null object
|
||||
*/
|
||||
createObjectOffsetList(list: string[] | any[]): Offset[] {
|
||||
createObjectOffsetList(list: (string | IGeneratedObject)[]): Offset[] {
|
||||
const ret: number[] = [];
|
||||
|
||||
for(let i = 0; i < list.length; ++i) {
|
||||
@@ -583,7 +583,7 @@ export class Builder {
|
||||
return ret;
|
||||
}
|
||||
|
||||
createStructOffsetList(list: string[] | any[], startFunc: (builder: Builder, length: number) => void): Offset {
|
||||
createStructOffsetList(list: (string | IGeneratedObject)[], startFunc: (builder: Builder, length: number) => void): Offset {
|
||||
startFunc(this, list.length);
|
||||
this.createObjectOffsetList(list.slice().reverse());
|
||||
return this.endVector();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { FILE_IDENTIFIER_LENGTH, SIZEOF_INT } from "./constants.js";
|
||||
import { int32, isLittleEndian, float32, float64 } from "./utils.js";
|
||||
import { Offset, Table, IGeneratedObject } from "./types.js";
|
||||
import { Offset, Table, IGeneratedObject, IUnpackableObject } from "./types.js";
|
||||
import { Encoding } from "./encoding.js";
|
||||
|
||||
export class ByteBuffer {
|
||||
@@ -257,33 +257,31 @@ export class ByteBuffer {
|
||||
/**
|
||||
* A helper function for generating list for obj api
|
||||
*/
|
||||
createScalarList(listAccessor: (i: number) => unknown, listLength: number): any[] {
|
||||
const ret: any[] = [];
|
||||
createScalarList<T>(listAccessor: (i: number) => T | null, listLength: number): T[] {
|
||||
const ret: T[] = [];
|
||||
for(let i = 0; i < listLength; ++i) {
|
||||
if(listAccessor(i) !== null) {
|
||||
ret.push(listAccessor(i));
|
||||
const val = listAccessor(i);
|
||||
if(val !== null) {
|
||||
ret.push(val);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A helper function for generating list for obj api
|
||||
* @param listAccessor function that accepts an index and return data at that index
|
||||
* @param listLength listLength
|
||||
* @param res result list
|
||||
*/
|
||||
createObjList(listAccessor: (i: number) => unknown, listLength: number): any[] {
|
||||
const ret: any[] = [];
|
||||
createObjList<T1 extends IUnpackableObject<T2>, T2 extends IGeneratedObject>(listAccessor: (i: number) => T1 | null, listLength: number): T2[] {
|
||||
const ret: T2[] = [];
|
||||
for(let i = 0; i < listLength; ++i) {
|
||||
const val = listAccessor(i);
|
||||
if(val !== null) {
|
||||
ret.push((val as IGeneratedObject).unpack());
|
||||
ret.push(val.unpack());
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
1
ts/flatbuffers.ts
Normal file
1
ts/flatbuffers.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * as flatbuffers from './index.js'
|
||||
@@ -3,7 +3,7 @@ export { SIZEOF_INT } from './constants.js'
|
||||
export { FILE_IDENTIFIER_LENGTH } from './constants.js'
|
||||
export { SIZE_PREFIX_LENGTH } from './constants.js'
|
||||
|
||||
export { Table, Offset } from './types.js'
|
||||
export { Table, Offset, IGeneratedObject, IUnpackableObject } from './types.js'
|
||||
|
||||
export { int32, float32, float64, isLittleEndian } from './utils.js'
|
||||
|
||||
|
||||
@@ -10,5 +10,8 @@ export type Table = {
|
||||
|
||||
export interface IGeneratedObject {
|
||||
pack(builder:Builder): Offset
|
||||
unpack(): IGeneratedObject
|
||||
}
|
||||
|
||||
export interface IUnpackableObject<T> {
|
||||
unpack(): T
|
||||
}
|
||||
Reference in New Issue
Block a user