TypeScript: Parse rows by a schema with conditional types

Read a table whose header says each column's type, and turn the rows into typed values, using a conditional type to link a column type to its values.

What to build

A program that parses rows by a schema and reports the rows that do not fit.

Requirements

  • Declare type ColumnType = "string" | "number" | "boolean" and a conditional type ValueOf<T> giving number, boolean or string.
  • Write parseCell<T extends ColumnType>(type: T, text: string): ValueOf<T> | null: a number column takes text that Number() turns into a number and is not empty; a boolean column takes true or false; a string column takes anything.
  • For each row print the row as JSON (keys in column order) or, at the first cell that does not fit, row R: COLUMN must be a TYPE (rows numbered from 1). Then print valid rows: V of N.

Input

Line 1: the header, columns name:type separated by commas. Line 2: N. Then N rows, cells separated by commas.

Output

N lines, then the count.

Constraints

1 to 10 columns; 1 ≤ N ≤ 50; every row has one cell per column.

Example

Input:

name:string,age:number,admin:boolean
3
Ada,36,true
Alan,forty,false
Grace,85,yes

Output:

{"name":"Ada","age":36,"admin":true}
row 2: age must be a number
row 3: admin must be a boolean
valid rows: 1 of 3

Why it works

The conditional type ties each column type to the kind of value it produces, so the parser's return type changes with its argument, and the JSON output shows real numbers and booleans rather than text.

Examples

Example 1 · Input
name:string,age:number,admin:boolean
3
Ada,36,true
Alan,forty,false
Grace,85,yes
Expected output
{"name":"Ada","age":36,"admin":true}
row 2: age must be a number
row 3: admin must be a boolean
valid rows: 1 of 3
Example 2 · Input
city:string,population:number
2
Dhaka,10200000
Oslo,
Expected output
{"city":"Dhaka","population":10200000}
row 2: population must be a number
valid rows: 1 of 2

Hints

Hint 1

type ValueOf<T extends ColumnType> = T extends "number" ? number : T extends "boolean" ? boolean : string.

Hint 2

parseCell returns the value or null when the text does not fit the column's type.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

type ColumnType = "string" | "number" | "boolean";
type ValueOf<T extends ColumnType> = T extends "number" ? number : T extends "boolean" ? boolean : string;

function isColumnType(text: string): text is ColumnType {
  return text === "string" || text === "number" || text === "boolean";
}

function parseCell<T extends ColumnType>(type: T, text: string): ValueOf<T> | null {
  if (type === "number") return text.trim() !== "" && !Number.isNaN(Number(text)) ? (Number(text) as ValueOf<T>) : null;
  if (type === "boolean") return text === "true" || text === "false" ? ((text === "true") as ValueOf<T>) : null;
  return text as ValueOf<T>;
}

const columns: { name: string; type: ColumnType }[] = read().split(",").map((c) => {
  const [name, type] = c.split(":");
  return { name, type: isColumnType(type) ? type : "string" };
});
const n = Number(read());
let valid = 0;
for (let r = 1; r <= n; r++) {
  const cells = read().split(",");
  const row: Record<string, string | number | boolean> = {};
  let problem = "";
  for (let c = 0; c < columns.length; c++) {
    const value = parseCell(columns[c].type, cells[c]);
    if (value === null) {
      problem = "row " + r + ": " + columns[c].name + " must be a " + columns[c].type;
      break;
    }
    row[columns[c].name] = value;
  }
  if (problem) console.log(problem);
  else {
    valid++;
    console.log(JSON.stringify(row));
  }
}
console.log("valid rows: " + valid + " of " + n);

Lesson: Mapped, conditional and template literal types →