Skip to content
Myr

untouchable.ts

This file

Path

packages/testkit/src/untouchable.ts

36 lines · 1.1 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 1why, one step back
1/**

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 2–4why, one step back
2 * Every read throws and names the member, so a test proves what a call
3 * touches. Symbols and `then` read as absent: printing a value or resolving a4 * promise with it is not a use.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 5–36why, one step back
5 */
6import { Predicate } from "effect";78export class TouchedError extends Error {9 constructor(name: string, member: string) {10 super(`a test touched ${name}.${member}`);11 this.name = "TouchedError";12 }13}1415export const untouchable = <T extends object>(16 name: string,17 known: Partial<T> = {}18): T => {19 const members = new Map(Object.entries(known));2021 return new Proxy<T>(22 // oxlint-disable-next-line typescript/no-unsafe-argument -- a fake whose every read throws has no members to type, so its target is a bare object; `known` holds the only members a caller sees.23 Object.create(null),24 {25 get: (_target, key) => {26 const member = Predicate.isString(key) ? members.get(key) : undefined;2728 if (member !== undefined || Predicate.isSymbol(key) || key === "then") {29 return member;30 }3132 throw new TouchedError(name, key);33 },34 }35 );36};