top | item 28287346

Ask HN: Resources on writing a parser in TypeScript's type system

1 points| PeledYuval | 4 years ago | reply

Hi HN, a niche Typescript+parsers question.

I'm looking for resources that will allow me to write a parser inside of Typescript's type system by leveraging template literal types.

Reasoning: I'm writing a Typescript library that allows compile-time checks for raw SQL queries. The goal is to allow compile time safety for queries without reliance on an ORM query builder or manually maintained type hints. It would look roughly like so:

  // some type of DB structure definition. This will definitely change
  const dbSchema = { person: {name: 'varchar', born_at: 'timestamptz' }};
  // Throws compile time error since "address" does not exist in "person" table
  query<typeof dbSchema>('SELECT address FROM person');
I have a (more complicated) POC of this here: https://github.com/Yuval-Peled/Tequel

Thanks in advance

EDIT: Punctuation

discuss

order

No comments yet.