google / google/flatbuffers

[TS] In version 2, why do union helpers use string comparison?

Open
#6,741 22 comments 1 reaction 0 assignees View on GitHub
good-first-contribution pr-requested typescript
Dominant language
C++
Stars
26.5k
Forks
3.7k
PR merge metrics
No merged PRs in 30d

Description

In version 2, union helpers in the form of `unionToAnyX` are generated. Inside them, there is a switch of type:
```typescript
switch(AnyX[type]) {
case 'NONE': return null;
...
}
```

Where `type` is the actual enum with variants defined as integers starting from 0. So, why not switching directly on `type`, like:
```typescript
switch(type) {
case '0' /* NONE */: return null;
...
}
```

as the code is generated anyway?

Moreover, it might make sense to use `const enum`s for performance reasons, if the added constraints on the API are acceptable.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.