Revise
Upon revisiting the original post, it appears that the inquiry is not exactly in line with what was initially assumed. The function is intended to take a singular nested value as a parameter, rather than the entire object. While the comments on the question hint at potentially restricting the parameter's type to only xxx
, there isn't a clear way to control its origin.
Despite this clarification, the initial response I provided could still prove beneficial to either the OP or others:
Suggested Revision
If you anticipate consistent formatting for the parameter structure, consider defining a type or interface for it:
interface Thing {
a: {
ROLE_a: string
},
b: {
ROLE_b: string
}
// etc.
}
function doSomething(param: Thing) {
// perform action
console.log(param.a.ROLE_a); // no errors expected
}
For more adaptability, a combination of generics along with template literal types + key remapping can be utilized:
type Things<Prefix extends string, Keys extends string> = {
[K in Keys]: {
[T in K as `${Prefix}_${K}`]: string
}
};
type ParamsType = Things<'ROLE', 'a' | 'b'>
function doSomething(param: ParamsType) {
// perform action
console.log(param.a.ROLE_a); // no errors expected
}
Access Playground Here