
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
TypeScript to C++ compiler.
To have an idea of how the converted code looks like, you can check the
tests/data-conversion/
directory.
Note that this is an ongoing research and not production ready, if you are looking for a compiler that works with real code, please check TypeScript2Cxx and lots of other options.
Install:
npm install -g compilets
Help:
βββ Compilets - 0.0.1-dev βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
$ compilets <command>
βββ General commands ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
compilets build [--target #0]
Build C++ project.
compilets gen [--root #0] [--target #0]
Generate a C++ project from TypeScript project.
Example:
mkdir example
cd example
echo 'class A {}' > main.ts
compilets gen
compilets build
./cpp-project/out/Debug/example
The reason this project was created despite the existences of all other ones, is that I believe the C++ and TypeScript languages have some subtle overlaps and there is a beautiful way to map TypeScript code to C++.
This project means to validate my ideas which I did not find anywhere else.
The Oilpan GC is a garbage collection library designed for V8, but also can be used as an independant library. Compilets uses Oilpan to implement the GC in translated C++ code.
Most existing TypeScript to C++ compilers manage the lifetime of objects with
std::shared_ptr, which is simple reference counting and leaks for cyclic
references.
With Compilets you can safely translate a cyclic referenced class to C++:
class LinkNode {
next?: LinkNode;
}
const a = new LinkNode();
const b = new LinkNode();
a.next = b;
b.next = a;
class LinkNode : public compilets::Object {
public:
cppgc::Member<LinkNode> next;
void Trace(cppgc::Visitor* visitor) const override {
visitor->Trace(next);
}
};
LinkNode* a = compilets::MakeObject<LinkNode>();
LinkNode* b = compilets::MakeObject<LinkNode>();
a->next = b;
b->next = a;
In TypeScript a function is also an Object, while it is trivial to use lambda
functions in C++ to represent functions objects, the lifetime of the function
and the objects captured in its closure still needs management. In the meanwhile
using std::function for everything would hurt the performance.
This is solved by using a custom C++ object to represent function expressions and arrow functions (i.e. the ones assigned to variables), while still using the plain C++ functions to represent the top-level function declarations.
For lifetime management, the function object is managed by Oilpan GC. And the function body is parsed so all the objects captured in the closure become hiddden members of the function object, managed by Oilpan GC too.
function Simple() {}
let simple = Simple;
let callback = () => { simple() };
void Simple() {}
compilets::Function<void()>* simple = compilets::MakeFunction<void()>(Simple);
compilets::Function<void()>* callback = compilets::MakeFunction<void()>([=]() -> void {
simple->value()();
}, simple);
std::variantThe union types in TypeScript are represented as std::variant in C++, for
example number | string becomes std::variant<double, std::string>.
For union types that includes undefined, the std::monostate is used to
represent the empty state.
std::optionalThe optional function parameters and class properties in TypeScript are
represented as std::optional in C++. For example func(arg?: boolean) becomes
func(std::optional<bool> arg).
null and undefinedUnlike JavaScript, there is no undefined state for variables in C++, it is
possible simulate undefined but it would be at the cost of performance.
Thus in Compilets both null and undefined are treated as null states of
types: std::nullopt for std::optional, std::monostate for std::variant,
std::nullptr for other pointer types. This will of course break some even
strictly typed code, and to avoid generating incorrect code, errors will be
thrown when the TypeScript code needs to strictly distinguish between null
and undefined.
The documentations of Oilpan GC (cppgc) can be found at:
You can get familiar with TypeScript's compiler APIs with following articles:
Tools below will help developement of this project:
This project is published under GPLv3 license, including the C++ files that are built into the final binary.
I'll change the license to a permissive one when I consider this project as a ready product.
It is discouraged to submit patches as this project is still an ongoing experiment. If you are still kind enough to fix bugs, please note that you must agree to allow me to re-license the contributions to permissive licenses in future.
FAQs
Compile TypeScript to C++ Executables
The npm package compilets receives a total of 2 weekly downloads. As such, compilets popularity was classified as not popular.
We found that compilets demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: whatβs affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.