tusken icon indicating copy to clipboard operation
tusken copied to clipboard

Type brands for primary/foreign keys

Open aleclarson opened this issue 3 years ago • 0 comments

Goal: Extra type safety. Prevent primary keys for one table from being used in a query of another table.

// The generated type of a primary/foreign key
id: t.int4 & t.key<'user.id'>

To convert an external value into a branded key:

// Note: Falsy values are passed through.
const userId = t.user.id(req.searchParams.get('userId'))

Casting like what's shown above doesn't include runtime validation. This is only for type safety.

aleclarson avatar Oct 07 '22 15:10 aleclarson