mmomtchev / swig

This is SWIG JavaScript Evolution, a fork of the SWIG project with modern JavaScript/TypeScript support including WASM and async
http://www.swig.org
Other
7 stars 0 forks source link

C++ enum not wrapped in enum in typescript? #51

Closed wangito33 closed 3 months ago

wangito33 commented 3 months ago

The improvement on js support is great. I'm just wondering why C++ enum is not wrapped in enum typescript.

mmomtchev commented 3 months ago

Difficult to say without the code

wangito33 commented 3 months ago

seniority.zip

swig -c++ -javascript -napi -typescript -module enumclass seniority.h

produces the typescript file below:

declare const _SWIG_enum_tag: unique symbol;

export const Seniority_Senior: Seniority;

export const Seniority_Subordinated: Seniority;

export const Seniority_SeniorSubordinated: Seniority;

export const Seniority_Max: Seniority;

export type Seniority = number & { readonly [_SWIG_enum_tag]: 'Seniority'; };
mmomtchev commented 3 months ago

I don't see the problem with this output?

wangito33 commented 3 months ago

This is not that readable. Didn't try with vs code to see if it integrates well. I currently use the -node -tstypes option which wraps the enum to typescript enum. It has its own problem, but is easy to read and use.

mmomtchev commented 3 months ago

Oh, you prefer TypeScript enums. It is not possible - TypeScript enums exist only in TypeScript - they disappear once the code is transpiled to JavaScript. If SWIG was producing TypeScript enums, those wouldn't have been usable from JavaScript.

wangito33 commented 3 months ago

OK. Thank you for having looked at it. It is indeed better to use union type with typescript.