Adding type safety to object IDs in TypeScript - Kravchyk's
I was creating a type for an options object of a function, one of the options properties would accept an ID of the item (string based UUID) or a special value, i.e. 'currentNode'.
Initially I would come up with something like this:
interface InsertOptions {
// Insert into the item by id or the active item
target: string | 'currentNode'
}
function insert(options: InsertOptions): void {
//
}
insert({ target: 'activeNode' }); // Passing a clearly unintended option value will not result in an error
...
Read more at kravchyk.com