Advanced TypeScript for Harness Builders

20. `ReadonlyArray`, `Pick` Facades & Immutable APIs

Expose read-only views to prevent mutation of internal state.

Expose read-only views to prevent mutation of internal state.

File: packages/coding-agent/src/core/slash-commands.ts L17-38

export const BUILTIN_SLASH_COMMANDS: ReadonlyArray<BuiltinSlashCommand> = [
  { name: "settings", description: "Open settings menu" },
  { name: "model", description: "Select model" },
  // ...
];

Pick for read-only facades:

File: packages/coding-agent/src/core/session-manager.ts L184-199

export type ReadonlySessionManager = Pick<
  SessionManager,
  | "getCwd"
  | "getSessionDir"
  | "getSessionId"
  | "getSessionFile"
  | "getLeafId"
  | "getLeafEntry"
  | "getEntry"
  | "getLabel"
  | "getBranch"
  | "getHeader"
  | "getEntries"
  | "getTree"
  | "getSessionName"
>;

Extensions receive ReadonlySessionManager instead of the full SessionManager, blocking access to mutation methods.

File: packages/coding-agent/src/core/agent-session.ts L846-857

get scopedModels(): ReadonlyArray<{ model: Model<any>; thinkingLevel?: ThinkingLevel }> {
  return this._scopedModels;
}

get promptTemplates(): ReadonlyArray<PromptTemplate> {
  return this._resourceLoader.getPrompts().prompts;
}

Open this chapter inside the full course