@arki/ts for agents
For @arki/[email protected] · source docs/agent-guide.md
The long form of llms.txt. When they disagree, src/index.ts wins.
The model
- Most of the package is types. Types cost nothing at runtime.
- Three small runtime helpers exist:
once,lazyObject,resolveLazy.expectImportPurelives on@arki/ts/testingand needs Node. - Every type has an
expectTypeOfassertion insrc/types.test-d.ts. Read it to see the exact result. - The helpers cover plain objects and string keys. They are small on purpose.
Task map
Type a lookup by dotted path
- Build the union of valid paths with
RecordToPath<typeof value>. - Constrain a generic to it:
P extends RecordToPath<T>. - Return
PathValue<T, P>.
import { expectTypeOf } from 'vitest';
import type { PathValue, RecordToPath } from '@arki/ts';
const flags = { beta: { search: true, chat: false } } as const;
declare function flag<P extends RecordToPath<typeof flags>>(path: P): PathValue<typeof flags, P>;
expectTypeOf(flag('beta.search')).toEqualTypeOf<true>();Convert API keys between snake_case and camelCase
- Use
DeepSnakeToCamel<T>for a response from a snake_case API. - Use
DeepCamelToSnake<T>for a request body. - The runtime conversion is your code. The type only describes it.
import { expectTypeOf } from 'vitest';
import type { DeepSnakeToCamel } from '@arki/ts';
type ApiUser = { user_name: string; home_address: { zip_code: string } };
expectTypeOf<DeepSnakeToCamel<ApiUser>>().toEqualTypeOf<{ userName: string; homeAddress: { zipCode: string } }>();Build a shared object at import time without side effects
- Wrap the construction in
lazyObject(() => create(...)). - Export the result. Reading a property builds it, once.
- Prove it with
expectImportPurefrom@arki/ts/testing.
Make a config field take a value or a function
- Type it
Lazy<T>. - Call
resolveLazy(field)where the value is needed.
Rules that bite
RecordToPathstops at arrays. There is no'list.0'.PathValuerejects a path that is not inRecordToPath. The error is TS2344.DeepPartialalso recurses into functions and class instances.CamelToSnakedrops number and symbol keys.SnakeToCamelkeeps them.onceretries after a synchronous throw. It remembers a rejected promise.@arki/ts/testingspawns a subprocess. Do not import it in a browser bundle.- Type assertions run in
bun run typecheck, not inbun run test.
Errors
| Code | Meaning | What to do |
|---|---|---|
| none | The package defines no error codes. | Types fail at compile time. Runtime helpers throw what your callback throws. |
TS2344 | A path is not in RecordToPath<T>. | Fix the path. The message lists the valid ones. |
TS2589 | A deep helper hit the compiler's depth limit. | Flatten the input type or apply the helper per branch. |
Verify
bun run typecheck
bun run test
bun run test:docs
bun ../../tooling/library/src/llms.ts --check