Skip to content
/ TypeScriptXX Public template
forked from TheLartians/TypeScriptXX

Stay safe! Type-safe scripting for C++ using TypeScript and CMake with auto-generated declarations.

License

Notifications You must be signed in to change notification settings

zeta1999/TypeScriptXX

 
 

Repository files navigation

Actions Status Actions Status

TypeScriptXX

Example project showing how to embed the TypeScript scripting language in a C++ project with automatically generated declarations.

Motivation

Lua is a great embedded scripting language for C++ projects. Unfortunately, Lua code becomes unmaintainable as the project grows, due to the absence of reliable static type checkers. In the JavaScript world this was solved a long time ago with through the TypeScript language. Thanks to the great work of TypeScriptToLua project, it is now possible to use TypeScript for Lua projects. This project demonstrates how to use CMake to bring TypeScript, Lua and C++ together and can be used as inspiration or a starting point for further projects.

Usage

git clone https://github.com/TheLartians/TypeScriptXX.git
cd TypeScriptXX
cmake -H. -Bbuild
cmake --build build -j8 # compiles C++, updates declarations and transpiles TypeScript
./build/TypeScriptXX -s lua # runs the transpiled lua script lua/index.lua
npx concurrently "npm run watch" "./build/TypeScriptXX -s lua --watch" # runs the transpiler and program in watch mode

How it works

The C++ project is contained in source/main.cpp which contains the C++ library and a Lua interface. It also defines a simple CLI for generating and printing the declarations and executing a lua script. The main TypeScript script is located at typescript/index.ts.

The CMake script uses CPM and npm to download and configure all dependencies in the current directory. After building the C++ code, CMake uses the executable to update the TypeScript declarations (at typescript/cpplib.d.ts) and then transpiles the TypeScript code to Lua with TypeScriptToLua. The Lua code is then located in the lua directory at the project's root.

The script also adds package.json and the TypeScript sources to the dependency to automatically re-invoke TypeScriptToLua on changes. The TypeScript sources are collected using GLOB, so be sure to re-run cmake after adding new source files.

Additional commands

Recompile TypeScript without invoking CMake

npm run build

About

Stay safe! Type-safe scripting for C++ using TypeScript and CMake with auto-generated declarations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CMake 74.1%
  • C++ 22.2%
  • TypeScript 3.7%