TypeScript: assert an object literal has key-values that equal each other TypeScript: assert an object literal has key-values that equal each other typescript typescript

TypeScript: assert an object literal has key-values that equal each other


Not with a single type, you can do it with a function:

function propAsValue<T extends { [P in keyof T]: P }>(o: T) {    return o;}const testIds = propAsValue({    foo: 'foo'});const testIds2 = propAsValue({    foo: 'bar'});

Playground Link

Or with an inline function, if you want to be terse and confuse everyone:

const testIds = (<T extends { [P in keyof T]: P }>(o: T) => o)({    foo: 'foo'});

Although I am not sure what your use case for this is, you might be better off with using Object.keys.


Yes you can get the object keys with Object.keys then you can loop through the keys and compare the string values. If they do not equal you can throw an error.