Skip to main content

Custom Types (...any T)

Nitrogen can ✨automagically✨ generate custom types and their respective bindings for any types used in your specs.

Custom interfaces (structs)

Any custom interface or type will be represented as a fully type-safe struct in C++/Swift/Kotlin. Simply define the type in your .nitro.ts spec:

Nitro.nitro.ts
interface Person {
name: string
age: number
}

interface Nitro extends HybridObject {
getAuthor(): Person
}
HybridNitro.swift
class HybridNitro: HybridNitroSpec {
func getAuthor() -> Person {
return Person(name: "Marc", age: 24)
}
}



Nitro enforces full type-safety to avoid passing or returning wrong types. Both name and age are always part of Person, they are never a different type than a string/number, and never null or undefined.

This makes the TypeScript definition the single source of truth, allowing you to rely on types! 🤩

Enums (TypeScript enum)

A TypeScript enum is essentially just an object where each key has an incrementing integer value, so Nitrogen will just generate a C++ enum natively, and bridges to JS using simple integers:

enum Gender {
MALE,
FEMALE
}
interface Person extends HybridObject {
getGender(): Gender
}

This is efficient because MALE is the number 0, FEMALE is the number 1, and all other values are invalid.

Enums (TypeScript union)

A TypeScript union is essentially just a string, which is only "typed" via TypeScript.

type Gender = 'male' | 'female'
interface Person extends HybridObject {
getGender(): Gender
}

Nitrogen statically generates hashes for the strings "male" and "female" at compile-time, allowing for very efficient conversions between JS strings and native enums.