Suppose we have an object literal defined as:
export const SOURCE = {
UNKNOWN: 'Unknown',
WEB: 'Web',
MOBILE: 'Mobile',
...
} as const;
and
export const OTHER_SOURCE = {
UNKNOWN: 0,
WEB: 1,
MOBILE: 2, // values may not be sequential or incrementing
...
} as const;
OTHER_SOURCE.UNKNOWN // inferred as 0
Is there a way to restrict the second object literal so that it uses keys from SOURCE
but keeps the values as literals, instead of numbers like in the example below:
type ConstraintType = {
[key in keyof typeof USER_SOURCE]: number; // how can we infer (and possibly enforce numeric values) from the second object?
};
export const OTHER_SOURCE: ConstraintType = { ... } as const;
OTHER_SOURCE.UNKNOWN // inferred as a number ]
Appreciate any assistance and explanations.