Share code between iOS and Android
One module with a platform branch per SDK call; platform files only when the halves share nothing.
import { PLATFORM } from "lucent:platform";import { UIDevice } from "lucent:ios/UIKit";import { Build, Build_VERSION } from "lucent:android/android.os";import { main } from "lucent:thread";
export type DeviceInfo = { platform: string; model: string; label: string };
// Shared code: plain Lucent.function label(model: string, version: string): string { return `${model} (${version})`;}
export async function deviceInfo(): Promise<DeviceInfo> { if (PLATFORM === "ios") { const [model, version] = await main(() => [ UIDevice.current.model, UIDevice.current.systemVersion, ]); return { platform: PLATFORM, model, label: label(model, `iOS ${version}`) }; } else { const model = Build.MODEL ?? "Android"; return { platform: PLATFORM, model, label: label(model, `Android ${Build_VERSION.RELEASE ?? ""}`), }; }}if (PLATFORM === "ios") { … } else { … }compiles each branch for its own platform.? :,switch (PLATFORM)andPLATFORM === "ios" && …work too.- A top-level function, class or variable that uses one platform's SDK outside a branch belongs to that platform, and compiles only there.
- Exports run on both platforms, so they branch inside.
- Both branches are type-checked on every build. Where a platform's SDK isn't installed, its code is untyped and skipped.
A guard clause works too, as TypeScript narrows it: after if (PLATFORM === "ios") return …; the rest of the block is Android code. The test must be PLATFORM alone (not PLATFORM === "ios" && ready), with no else, and its branch must end in return, throw, break or continue.
When to split into platform files
Section titled “When to split into platform files”When the two halves share nothing, split the module: x.lucent.ts holds only export declare functions and types, and x.ios.lucent.ts and x.android.lucent.ts each implement all of them (LUCENT3005). JavaScript imports x.lucent either way.