TaskKit wants to be an app on a phone and a window on a desktop. See the two ways TypeScript leaves the browser, read a React Native screen and the typed bridge between an Electron app's two programs, and keep the channel names between them checked.
TypeScript
Types
Mobile and desktop apps. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Your friend uses TaskKit's page every day now. On the train this
morning the signal dropped, and the page would not open. "Can it be
an app?" they ask. "An icon on my phone, and on my laptop a window
of its own that keeps the list in a file, even with no internet."
A page lives in a browser tab, and a tab can do neither. But the
code behind it can: the same TypeScript, and often the same React,
runs in phone apps and desktop apps every day. An app's code is
installed on the device, so it opens with no signal. In this lesson
you see the two ways TypeScript gets there and which tool takes
which way, and you read the typed code of two of them: a screen of
a React Native app on the phone, and the bridge between an Electron
app's two programs.
Two ways out of the browser
Every app has to answer two questions. Where does it run: on a
phone, or on a desktop computer? And who draws its screen?
One answer to the second question is your page itself. The app holds a web
view: a browser with no address bar or tabs, built into the app and filling
its window. Your HTML (HyperText Markup Language, the page's tags), your CSS
(Cascading Style Sheets, the rules for how they look) and your TypeScript run
in it as they do in a browser, and the app adds what a tab cannot have, such
as an icon and a way to the computer's files. The other answer is native
components: the system's own text, buttons and lists, the ones every other
app on that phone or computer is built from. Your code still decides what is
on the screen, but no HTML is involved.
The tools you will meet are sorted by those two answers:
Where it runsYour page, in a web viewNative componentsPhone
Capacitor
Your web page inside an iPhone or Android app.
ReactNative
Your React components, drawn with native components.
Desktop
Electron,Tauri
Your page in a window, beside a program that reaches the disk.
ReactNativeforWindowsandmacOS
The same, on desktops, made by Microsoft.
What draws the screen
Each tool answers two questions: phone or desktop, and does
your page draw the screen or native components
Capacitor is the shortest way to a phone: it packs the page you
already have into an iPhone and an Android app, and gives your
TypeScript typed functions for what only a phone has, such as the
camera. React Native for Windows and macOS, made by Microsoft,
takes React Native's way to the desktop. The two orange cells hold
the two tools the rest of this lesson reads: React Native for the
phone, Electron for the desktop.
React Native: React without HTML
React Native is React for phones. You write components, props
and state exactly as lesson A1.2 taught, in .tsx files, and React keeps the
screen matching your data. What changes is the tags. A phone has no
<li> or <div>; React Native gives you native components,
imported from the react-native package:
<View> holds others, <Text> shows text, and <TextInput> is a
box to type in.
web/TaskItem.tsx
<li> {mark} {task.title}</li>
The page
Does: Draws an HTML list item in a browser
Never: Runs where there is no browser
mobile/TaskItem.tsx
<Text> {mark} {task.title}</Text>
The phone
Does: Draws the phone's own text, with Text
Never: Has HTML tags such as <li> or <div>
The same component: the page draws an li, the phone draws
Text
The check will not stop a <div> in a React Native file: React's
types still know HTML tags, so the mistake shows up only when the
app runs.
React Native's own docs recommend starting a new app with a
framework, and the one they name is Expo (reactnative.dev,
"Get Started", checked on 26 November 2026; this may change). Expo
is a set of tools that builds the app and runs it on your phone
while you work, through Expo Go, an app you install on the phone.
One command makes a project. Run it from taskkit, and when it
asks "What is your app named?", answer mobile. When it then asks
for an Expo SDK (software development kit) version, the set of
Expo's libraries the app is built on, press Return to take the
latest. @latest asks
npm for the newest version of the tool, and
--templateblank-typescript picks the smallest project, written
in TypeScript:
TypeScript helps most where your habits from the web do not fit a
phone. React Native's components come with their own types, so the
check knows what each one accepts. Here is TaskKit's box for a new
task, written the way lesson A1.2 wrote it for the page:
mobile/NewTask.tsxtsx
import { useState } from "react";import { TextInput } from "react-native";export function NewTask() { const [text, setText] = useState(""); return ( <TextInput placeholder="A task" value={text} onChange={(event) => setText(event.target.value)} /> );}
On the page, event.target was the <input>, and its value the
text. A phone's text box sends a different event, and the check says
so before the app ever starts. From taskkit, check the mobile
project (-p, short for project, as in lesson A1.4):
bash
npx tsc -p mobile
Outputtext
mobile/NewTask.tsx:10:36 - error TS18048: 'event.target' is possibly'undefined'.10 onChange={(event) => setText(event.target.value)} ~~~~~~~~~~~~mobile/NewTask.tsx:10:49 - error TS2551: Property 'value' does notexist on type 'number | ReactNativeElement'. Did you mean 'valueOf'? Property 'value' does not exist on type 'number'.10 onChange={(event) => setText(event.target.value)} ~~~~~Found 2 errors in the same file, starting at: mobile/NewTask.tsx:10
Each error line is one long line in your terminal, broken here to
fit; the second one's wording comes from React Native 0.88's types
and may differ in another version. TextInput has a prop made for
this, onChangeText, which
calls your function with the new text itself, so setText can be
handed over as it is:
Electron puts your page in a desktop window, on Windows, macOS
and Linux. VS Code (Visual Studio Code), the editor you write this
course in, is an Electron app. Inside, an Electron app is two
processes, two programs running at once, working together:
the main process, a Node.js program. It opens the windows, and
it can do what Node can, such as read and write files;
the renderer, your page, running in the window's web view. Like
any page, it cannot touch the disk.
To save TaskKit's list in a file, the page has to ask the main
process. The two are separate programs, so they talk by sending
messages, called IPC, short for inter-process communication.
Each message has a channel, a name the main process answers to,
such as "save-tasks". A small third file, the preload, runs
before the page and gives it a few functions that send those
messages, a bridge to the main process; the page sees it as
window.taskkit. The main process names the preload when it opens
the window, in webPreferences: { preload }. Press Next
step to walk one save through the three, or let it play:
Page
Preload
Main
1
Page to Preload: taskkit.saveTasks(tasks)
2
Preload to Main: invoke("save-tasks",tasks)
3
Main, to itself: writeFile(…)
4
Main to Preload: done(novalue)
5
Preload to Page: thepromiseisfulfilled
The page asks the bridge, the bridge sends a message, and
only the main process writes the file
All 5 steps shown.Step 1 of 5: Page → Preload — taskkit.saveTasks(tasks)Step 2 of 5: Preload → Main — invoke("save-tasks", tasks)Step 3 of 5: Main — writeFile(…)Step 4 of 5: Main → Preload — done (no value)Step 5 of 5: Preload → Page — the promise is fulfilled
All 5 steps shown.
The page and the main process are two programs with a message
between them, which is lesson A1.4's page and server again. Each is
checked on its own, so nothing notices if the page calls a function
the preload does not offer. The answer is lesson A1.4's too: one interface,
in a shared file, that says what the bridge offers, beside the one
Task of shared/task.ts:
shared/api.tsts
import type { Task } from "./task.ts";export interface TaskKitApi { loadTasks: () => Promise<Task[]>; saveTasks: (tasks: Task[]) => Promise<void>;}
The preload builds a bridge that matches that interface and hands it
to the page.
ipcRenderer.invoke sends a message on a channel and returns a
promise of the answer; contextBridge.exposeInMainWorld puts the
object on the page's window, under the name it is given:
electron/preload.tsts
import { contextBridge, ipcRenderer } from "electron";import type { TaskKitApi } from "../shared/api.ts";const api: TaskKitApi = { loadTasks: () => ipcRenderer.invoke("load-tasks"), saveTasks: (tasks) => ipcRenderer.invoke("save-tasks", tasks),};contextBridge.exposeInMainWorld("taskkit", api);
ipcRenderer.invoke gives back Promise<any>, so the Task[] in
loadTasks is a claim nothing checks, as in lesson A1.4: the page
should pass what loadTasks returns through lesson A1.4's parseTasks
before it uses it.
The main process answers each channel with ipcMain.handle, using
Node's readFile, writeFile and existsSync as Level 1's 12.2
did. Its function gets the message's details first, event, and then
whatever the preload sent. The file goes in the folder the system
gives each app for its data, app.getPath("userData"); join from
node:path puts a folder and a file name together into one path:
electron/main.tsts
import { existsSync } from "node:fs";import { readFile, writeFile } from "node:fs/promises";import { join } from "node:path";import { app, ipcMain } from "electron";const file = join(app.getPath("userData"), "tasks.json");ipcMain.handle("load-tasks", async () => { if (!existsSync(file)) { return []; } return JSON.parse(await readFile(file, "utf8"));});ipcMain.handle("save-tasks", async (event, tasks) => { await writeFile(file, JSON.stringify(tasks));});
The page's check has never heard of window.taskkit: the browser's
own Window type has no such property. A short .d.ts file, the
kind Level 1's 9.3 met, adds it. declare global lets this file add
to a type every file of the page sees, and the interface Window
inside merges into the browser's own, the declaration merging of
Level 1's 6.5. Level 2's Ambient declarations, global types, and
.d.ts files explains the pattern in full:
web/global.d.tsts
import type { TaskKitApi } from "../shared/api.ts";declare global { interface Window { taskkit: TaskKitApi; }}
Now window.taskkit.saveTask(tasks) with a missing s fails the
page's check with Property 'saveTask' does not exist on type 'TaskKitApi'. Did you mean 'saveTasks'?.
The interface pays off a second time. The page's code needs only
something that is a TaskKitApi, not Electron itself, so it runs
with a stand-in, a small object that plays Electron's part, the way
a test would run it. This lesson's blocks
run one file at a time, so the shared type, the page's code and the
stand-in are in one block, each under a comment that names its part.
Press Run:
taskkit.tsts
// shared/task.ts and shared/api.tsinterface Task { title: string; completed: boolean;}interface TaskKitApi { loadTasks: () => Promise<Task[]>; saveTasks: (tasks: Task[]) => Promise<void>;}// web/tasks.ts: the page's code, which knows only TaskKitApiasync function addTask(api: TaskKitApi, title: string): Promise<number> { const tasks = await api.loadTasks(); await api.saveTasks([...tasks, { title, completed: false }]); return tasks.length + 1;}// A stand-in for Electron's bridge, as a test would write itlet saved: Task[] = [{ title: "Buy milk", completed: false }];const standIn: TaskKitApi = { loadTasks: async () => saved, saveTasks: async (tasks) => { saved = tasks; },};console.log(await addTask(standIn, "Call Mom"));console.log(saved.map((task) => task.title));
In the app, the page calls addTask(window.taskkit, text), and the
same function saves through the main process into tasks.json.
Tauri has the same shape: your page in a web view, and a program
beside it. It uses the web view the system already has instead of shipping a
browser of its own, so its apps are small; the program beside the page is
written in Rust, a programming language for fast programs that run without
Node, and Tauri 2 builds phone apps too. The page calls it with
invoke<Task[]>("load_tasks"), load_tasks being the Rust function's name,
and the same holds for that Task[] as for Electron's.
To start a project of your own, each tool has a command that makes
the folder. The first makes an Electron project, taskkit-desktop,
with --template=vite--typescript picking one built with Vite, in
TypeScript. The second makes a Tauri one and asks a few questions
first: npm create tauri-app runs the package create-tauri-app,
a project maker, as npx runs a tool:
The interface checks what the bridge offers the page. It cannot check the
channel names inside the bridge: to ipcRenderer.invoke, a channel
is any string, and its answer is Promise<any>.
Common mistake
The preload sends on a channel the main process never answers
The check passes, and the app starts. The first time the page loads
its tasks, the promise is rejected, and the page's console shows the
error above. The console is in the window's developer tools, opened
with View → Toggle Developer Tools or these keys:
⌥ Option+⌘ Cmd+I
Ctrl+Shift+I
Ctrl+Shift+I
The terminal where you started the app prints its side of it:
In the terminaltext
Error occurred in handler for 'load-task': [Error: No handlerregistered for 'load-task']
That is one line in your terminal, broken here to fit.
Error: Error invoking remote method 'load-task': Error: No handler registered for 'load-task'
The fix
Name the channels once, in shared/api.ts, as a union of literals,
and send every message through a small function that takes only
those:
Add to shared/api.ts:
shared/api.tsts
export type Channel = "load-tasks" | "save-tasks";
Now the typo fails the check, at its own line: Argument of type '"load-task"' is not assignable to parameter of type 'Channel'.
The main process answers through the same kind of function, so a
typo on its side fails too:
A message between two programs is a copy of the data with a name on
it, and no check reads both ends at once. Whenever a name crosses from one
program to another, a channel here or a route in lesson A1.3, write the
names once as a union, and let both sides use it.
Pass all three tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
The first two run on this page, with the parts in one block as
above. Press Run to try the code, and Check when you think it
works.
Exercise · Write the code
Add deleteTask to the bridge
TaskKit needs to remove a task. Add deleteTask, a function that
takes a task's title and returns a Promise<void>, to TaskKitApi,
and give the stand-in a deleteTask that keeps every task except
the one with that title.
Loading the editor…
Exercise · Fix the error
Fix a channel, add a channel
Here the main process is played by answers, one answer for each
channel. The preload sends on two channels: one name has a typo, and
the other is new. Fix the typo, then add "delete-task" to Channel
and give it the answer deleted, so the program prints three lines.
Loading the editor…
Exercise · Read the code
A div on a phone
A React Native file of TaskKit's has this line, written from the
habit of the page:
tsx
<div>{task.title}</div>
What happens?
Recap
Recap · 3 questions
Check yourself
What is the difference between Capacitor and React Native?
Check yourself
Why does the page save through the main process?
Check yourself
What does a shared TaskKitApi interface catch?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Mobile and desktop apps
What things are
Word
What it means
Web view
A browser inside an app, showing your page
Native components
The system's own text, buttons and lists
Capacitor
Your page, packed into a phone app
React Native
React for phones, with native components
Expo
Tools that build and run a React Native app
Electron
Your page in a desktop window, with Node beside it
Main process
Electron's Node program: windows and files
Preload
Runs before the page; builds its bridge
Renderer
The page in an Electron window, in a web view
Bridge
What the preload gives the page: window.taskkit
IPC
Inter-process communication: messages between processes
Channel
The name a message is sent on: "save-tasks"
Tauri
Like Electron, with Rust beside the page
Code
Write
To
<View>, <Text>
Hold others; show text, in React Native
onChangeText={setText}
Get a TextInput's new text
ipcRenderer.invoke(…)
Send a message on a channel; get a promise
ipcMain.handle(…)
Answer a channel in the main process
exposeInMainWorld(…)
Put the bridge on window
declare global
Add taskkit to the page's Window type
npx create-expo-app@latest
Start a React Native app with Expo
npx create-electron-app@latest
Start an Electron app
npm create tauri-app@latest
Start a Tauri app
How it runs
Rule
What it means
The page asks, main does
Only the main process touches the disk
One interface for the bridge
TaskKitApi in shared/api.ts
Names as a union
Channel, so a typo fails the check
Web habits break
event.target.value is not on a phone
Where to go next
You can now tell a web view from native components, say which tool
takes TaskKit to a phone or a desktop, and read the typed code of a
React Native screen and of the bridge between the two programs of an
Electron app.
Each of these tools is a course of its own; what you have is how
TypeScript fits into all of them.
Next is Scripts and command-line tools, where TypeScript runs with
no screen at all: a small program that does one job and ends, and a
command of TaskKit's own that you can type in any folder.
Before you move on, press Edit on the taskkit.ts block, and
add a copy of the stand-in whose loadTasks always fails:
loadTasks: async () => { throw new Error("Disk full"); }. Predict
what addTask does with it, then run it, and catch the error around
the call with try and catch.