Variants (A | B | ...
)
A Variant is a type of either one of the values defined in it's declaration. Example:
interface Math extends HybridObject {
distance(value: number | Point): number
}
tip
While variants are still very efficient, they need runtime-checks for type conversions, which comes with a tiny overhead compared to all other statically defined types. If possible, avoid variants.
Custom Alias Names
Each variant is a unique type in Swift/Kotlin - for example: string | number
becomes Variant_String_Double
.
Since the generated names are hard to read, it is recommended to declare type-aliases with custom names instead:
Bad ❌
export interface Math extends HybridObject {
calculate(): string | number
}
Good ✅
export type MathOutput = string | number
export interface Math extends HybridObject {
calculate(): MathOutput
}
This will then use the easier-to-read type-alias name instead of Variant_String_Double
:
nitrogen/generated/ios/HybridMathSpec.swift
public protocol HybridMathSpec: HybridObject {
func calculate() -> Variant_String_Double
func calculate() -> MathOutput
}