From signature to client

How a TypeScript function becomes a typed ArkTS call, with no schema file.

3 MIN READView Markdown ↗

One import, two files#

TypeScript
import { addNote } from './notes.api'

This one line means different things on each side. Bun loads notes.api.ts, your backend. ArkTS prefers .ets over .ts, so the device loads notes.api.ets, the client ArkAPI writes right next to it. The screen and the server share a name, never code.

Diagram
 pages/
 ├── notes.api.ts    ◄── Bun reads this     (your backend)
 ├── notes.api.ets   ◄── ArkTS reads this   (generated client)
 └── Notes.ets           import { addNote } from './notes.api'

Reading types with the real compiler#

generate opens every api file with ts-morph, a wrapper around the TypeScript compiler API (cli/src/generate.ts). It asks the type checker for each exported function's parameters and return type. It never parses source text, so type aliases, interfaces imported from another file and inferred return types all work.

Each type is then lowered to one ArkTS accepts (cli/src/lower.ts):

  • Interfaces defined in the api file are copied into the client.
  • Interfaces imported from another file are imported from that same file, so the screen and the server share one type, not two look-alikes.
  • string | null, arrays, Record<string, T> and optional trailing parameters carry over.
  • any, Date, Map, classes and functions stop generate, naming the type and what to use instead (Date → epoch milliseconds).

One analysis, three outputs#

Diagram
                         ┌─► notes.api.ets       typed client for ArkTS
 notes.api.ts ─ ts-morph ┼─► arkapi-schema.json  argument checks on the server
                         └─► fingerprint         "this app is out of date"
  • The client: one async function per endpoint, one onX listener per event, the interfaces it needs and the backend address.
  • The schema: before your function runs, the server checks every argument against it. A wrong call never reaches your code; it gets 400 args[0].title: expected string, got number, with the exact path.
  • The fingerprint: a hash of each file's schema, sent with every call. If you changed the backend but not the installed app, the server marks its answers and the device logs this app was built against a different version of the backend, once.

Names do the configuration#

  • The endpoint id is the file path plus the name: pages/chat/chat.api.ts → pages.chat.chat.sendMessage (cli/src/namespace.ts). No route table.
  • The name decides the kind: get, list, find, search, count, has, is or load followed by an uppercase letter is a query, anything else a mutation (cli/src/query.ts). getMessages can back a live screen; getaway cannot.
  • An exported event<T>() is named after its export when the backend loads, the same way functions are, and the client gets onTyping for typing.

The rename that breaks the build#

Because the client is generated from the signature, a backend change shows up where it matters: in the ArkTS compiler.

Diagram
 notes.api.ts           notes.api.ets              Notes.ets
 ────────────           ─────────────              ─────────
 text ─► body   ──gen──► interface Note {   ──────► note.text
                           body: string            ✗ Property 'text' does
                         }                           not exist on type 'Note'

With a hand-written client, the same rename compiles and every note shows up blank.

Tab to navigate results · Enter to open · Esc to close