I have a set of static identifiers that I want to use to tag function calls. Instead of simply passing the identifiers as arguments, I would like to ensure that each identifier is unique and throws an error if the same identifier is passed more than once:
type Id = 'id-1' | 'id-2';
function foo(id : Id) {
console.log(`my id: ${id}`);
}
foo('id-1');
foo('id-2');
Is there a way to create a magic type in TypeScript that enforces uniqueness for the identifiers passed to the function?
type MagicId = /* some magic type to achieve uniqueness */;
function foo(id : MagicId) {
console.log(`my id: ${id}`);
}
foo('id-1');
foo('id-1'); // should throw a type error