Here is my "normal" typing test:
const prop1 = 'property1';
const prop2 = 'property2';
type Type1 = Record<typeof prop1, string> & Record<typeof prop2, number>;
let obj1: Type1;
console.log(obj1.property1 === '1') // ok
console.log(obj1.property2 === 2) // ok
console.log(obj1.property1 === 1) // error : This condition will always return 'false' since the types 'number' and 'string' have no overlap.
console.log(obj1.property2 === '2') // error : This condition will always return 'false' since the types 'number' and 'string' have no overlap.
Here is my "dynamic" typing test; the only difference is the way I give property names:
const prop1_ = prop1 + '_';
const prop2_ = prop2 + '_';
type Type2 = Record<typeof prop1_, string> & Record<typeof prop2_, number>;
let obj2: Type2;
console.log(obj2.property1_ === '1') // no error : ok
console.log(obj2.property2_ === 2) // no error : ok
console.log(obj2.property1_ === 1) // !!! missing error !!!
console.log(obj2.property2_ === '2') // !!! missing error !!!
Is there a way to make typescript manage my dynamically named property?