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.

TypescriptCli Tooling

Code

typescriptsnippet
24 lines
// 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: true

Runtime 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

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.