Leading And Middle Rest Slots In Tuple Types
Type variadic prefixes and packet interiors with leading and middle rest elements in labeled tuples so fixed header and trailing targets stay strongly typed.
Code
// Leading rest: arbitrary flags followed by one required output target
type CliCommand = [...flags: string[], target: string];
// Middle rest: fixed header, variable metric readings, fixed trailing checksum
type TelemetryFrame = [header: string, ...readings: number[], checksum: number];
function parseCliCommand(args: CliCommand): { flags: string[]; target: string } {
const target = args[args.length - 1] as string;
const flags = args.slice(0, -1);
return { flags, target };
}
function verifyFrame(frame: TelemetryFrame): boolean {
const [header, ...rest] = frame;
const checksum = rest[rest.length - 1];
const readings = rest.slice(0, -1);
const sum = readings.reduce((acc, n) => acc + n, 0);
console.log('frame header:', header, 'readings:', readings.join('+'), 'expected:', sum);
return sum === checksum;
}
const cmd = parseCliCommand(['--minify', '--sourcemap', 'dist/bundle.js']);
console.log('cli flags:', cmd.flags.join(' '), '| target:', cmd.target);
console.log('frame valid:', verifyFrame(['TEMP', 18, 22, 20, 60]));Output
Captured from a sandboxed run of this snippet in the build pipeline.
cli flags: --minify --sourcemap | target: dist/bundle.js
frame header: TEMP readings: 18+22+20 expected: 60
frame valid: trueRuntime walkthrough — Leading And Middle Rest Slots In Tuple Types
Trace execution beginning inside callables parseCliCommand and verifyFrame, doing all the work.
The result comes back as an object carrying flags.
What to watch for
- No lockfile entry, no version pinning — the file needs nothing beyond the runtime.
Putting it to use
Primary method header defines function parseCliCommand(args: CliCommand)
Output lines are prefixed (frame header:, cli flags:, frame valid:), tethering printed diagnostic text cleanly to each execution stage.
The call returns flags.
Core lines
const sum = readings.reduce((acc, n) => acc + n, 0);
return { flags, target };
return sum === checksum;
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.OpenAPI Endpoint ExplorerExplore OpenAPI 3.x YAML and JSON specifications locally with searchable endpoint tables, parameter summaries, security info, and cURL templates.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does `parseCliCommand` modify its input?
- No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
- Which packages does this snippet need?
- None. Every symbol the code uses resolves inside the standard library. The file defines `parseCliCommand`, `verifyFrame` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.