Flatbuffers version:master
OS: Mac OS BigSur
If I wanted to create a flatbuffers table that represents the following TS type:
type Foo = {
name?: string;
}
which is the same as:
type Foo = {
name: string | undefined;
}
..., I would define the following table, which represents a type with an optional field name of type string.
table Foo {
name:string;
}
When generating the TS code of Foo table, the obtained code is as follows:
export class Foo {
..
..
..
name():string|null
name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
name(optionalEncoding?:any):string|Uint8Array|null {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
}
The return type of name() method is string|null, whereas string|undefined would perfectly match the optional fields defined in TS as exposed above.
I'm finding myself manually converting types due to this. It would be great to have a flatc option to indicate that optional field getters should return undefined instead of null if a value is not set.
Is there any reason for not having this?
Would you be willing to accept a contribution for this feature request?
Flatbuffers version:master
OS: Mac OS BigSur
If I wanted to create a flatbuffers table that represents the following TS type:
which is the same as:
..., I would define the following table, which represents a type with an optional field
nameof typestring.When generating the TS code of
Footable, the obtained code is as follows:The return type of
name()method isstring|null, whereasstring|undefinedwould perfectly match the optional fields defined in TS as exposed above.I'm finding myself manually converting types due to this. It would be great to have a
flatcoption to indicate that optional field getters should returnundefinedinstead ofnullif a value is not set.Is there any reason for not having this?
Would you be willing to accept a contribution for this feature request?