Proposal: Support for registering custom TypeScript type strings in Embind #25145
Replies: 3 comments
-
Beta Was this translation helpful? Give feedback.
-
You could potentially use the current |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. AFAICT, Problem
What I want Given: enum class SomeEnum { a, b };
struct SomeClassWithEnum {
SomeEnum someEnum;
};
registerEnumType<SomeEnum>("SomeEnum", {"a", "b"});
class_<SomeClassWithEnum>("SomeClassWithEnum")
.property(&SomeClassWithEnum::someEnum); I’d like the generated typings to include: type SomeEnum = "a" | "b";
export interface SomeClassWithEnum extends ClassHandle {
readonly someEnum: SomeEnum;
} What I did before In 4.0.8 I (hackily) printed into the type file via stdout: template <typename T>
void registerEnumType(const char* name, const std::vector<std::string>& strings) {
if (isRunAsPartOfCompiling()) {
fmt::print("type {} = \"{}\";\n", name, fmt::join(strings, "\" | \""));
}
emscripten::register_type<detail::EnumOf<T>>(name);
} 4.0.9 broke this approach by stopping stdout from ending up in the typings. Proposal Provide an official API to append raw TypeScript to the generated definitions, e.g.: // Hypothetical API
namespace emscripten {
template <typename... Args>
void print_type_string(fmt::format_string<Args...> fmt, Args&&... args);
} Then template <typename T>
void registerEnumType(const char* name, const std::vector<std::string>& strings) {
emscripten::print_type_string("type {} = \"{}\";\n", name, fmt::join(strings, "\" | \""));
emscripten::register_type<detail::EnumOf<T>>(name);
// ...
} Alternative Change Either solution would cover the core need: a stable, free-standing alias in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I’d like to propose adding a way to provide custom TypeScript type strings in Embind. At the moment, the generated
.d.ts
output is useful, but there’s no mechanism to override or extend the type definitions when the default modeling isn’t sufficient.For example, I’m not satisfied with the behavior of
enum_
and have created my own helperregisterEnumType
that maps a C++ enum to runtime values. On the TypeScript side, I’d like this to generate a declaration such as:I can handle the runtime mapping by specializing
BindingType
andTypeID
, but there’s currently no way to inject the corresponding type string into the generated definitions.What I’m suggesting is a simple hook or API like:
This would not affect existing users or change default behavior, but it would give developers a way to supply precise type strings where needed.
Would such an extension point be acceptable? If yes, I can prepare a patch. If not, is there another mechanism that could achieve this?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions