Nitro's Typing System
Nitro uses an extensible typing system to efficiently convert between JS and C++ types - statically defined and fully type-safe and null-safe at compile-time.
For example, a JS number
will always be a double
on the native side:
interface Math extends HybridObject {
add(a: number, b: number): number
}
class HybridMath : public HybridMathSpec {
double add(double a, double b);
}
Nitro strictly enforces type-safety and null-safety - both at compile-time and at runtime.
This prevents accidentally passing a wrong type to add(..)
(for example, a string
) and performs null-checks to prevent passing and returning null
/undefined
values.
On the JS side (TypeScript), type- and null-safety is enforced via TypeScript - so use it!
Nitrogen
Nitrogen ensures that TypeScript definitions are always in sync with native type definitions. You can also use Nitro without nitrogen, in this case TypeScript definitions have to be written manually.
Supported Types
These are all the types Nitro supports out of the box:
JS Type | C++ Type | Swift Type | Kotlin Type |
---|---|---|---|
number | double / int / float | Double | Double |
boolean | bool | Bool | Boolean |
string | std::string | String | String |
bigint | int64_t / uint64_t | Int64 | Long |
T[] | std::vector<T> | [T] | Array<T> / PrimitiveArray |
T? | std::optional<T> | T? | T? |
[A, B, ...] | std::tuple<A, B, ...> | (A, B) 🟡 (#38) | ❌ |
A | B | ... | std::variant<A, B, ...> | Variant_A_B_C | Variant_A_B_C |
(T...) => void | std::function<void (T...)> | @escaping (T...) -> Void | (T...) -> Unit |
(T...) => R | std::function<std::future<R> (T...)> | ❌ | ❌ |
Record<string, T> | std::unordered_map<std::string, T> | Dictionary<String, T> | Map<String, T> |
Error | std::exception_ptr | Error | Throwable |
Promise<T> | std::shared_ptr<Promise<T>> | Promise<T> | Promise<T> |
object | std::shared_ptr<AnyMap> | AnyMapHolder | AnyMap |
ArrayBuffer | std::shared_ptr<ArrayBuffer> | ArrayBufferHolder | ArrayBuffer |
..any HybridObject | std::shared_ptr<HybridObject> | HybridObjectSpec | HybridObject |
..any interface | T | T | T |
..any enum | T | T | T |
..any union | T | T | T |