-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Obscure "type cannot be used to index type" error when a mapped type is nested into another mapped type #54289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Also, I found out that it's possible to kinda suppress the error by using the |
Lastly, I tried to explicitly declare that all steps of all entities should have a type Values<T extends object> = T[keyof T]
type Config = {
readonly [entityName: string]: {
readonly [stepName: string]: {
readonly body: object
}
}
}
const ENTITY_CONFIG = {
'entity1': {
'step1': {
body: { aaa: 1 }
},
'step2': {
body: { bbb: 1 }
},
},
'entity2': {
'step1': {
body: { ccc: 1 }
},
'step2': {
body: { ddd: 1 }
},
}
} satisfies Config
type EntityConfig = typeof ENTITY_CONFIG
type EntityType = Values<{
[T in keyof EntityConfig]: {
entityName: T,
step: Values<{
[S in keyof EntityConfig[T]]: {
stepName: S
body: EntityConfig[T][S]['body']
}
}>
}
}> |
It's likely a duplicate of #21760 |
Thank you for the quick answer, @Andarist! It really looks like a related problem. It's very sad that there's no visible success in finding a solution in this area. Could you please also let me know if you're aware of any other (maybe better) workarounds except the double |
You could try using something like this if you think that it makes your life easier (TS playground): type Tail<T extends any[]> = T extends [any, ...infer R] ? R : never;
type GetProp<T, K> = K extends keyof T ? T[K] : never;
type GetPath<T, P extends any[]> = P extends [any]
? GetProp<T, P[0]>
: GetPath<GetProp<T, P[0]>, Tail<P>>;
type EntityType = Values<{
[T in keyof EntityConfig]: {
entityName: T;
step: Values<{
[S in keyof EntityConfig[T]]: {
stepName: S;
body: GetPath<EntityConfig, [T, S, "body"]>;
};
}>;
};
}>; |
Thank you for the advice, @Andarist! I'll think about it — it looks great from the usage side, but the mutually-recursive implementation... oh, gods. 🥲 |
I'm not sure whether it will be proper to close this issue now, but feel free to do it if it is. |
I don't have such privileges - I think that the issue can be closed though (since it's a duplicate). Might be worth posting your case in that main thread though, this way it's likely that it will be added as a test case when that issue gets fixed. |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I see an error around the
EntityConfig[T][S]['body']
type expression with the following error message:Type '"body"' cannot be used to index type '{ readonly entity1: { readonly step1: { readonly body: { readonly aaa: 1; }; }; readonly step2: { readonly body: { readonly bbb: 1; }; }; }; readonly entity2: { readonly step1: { readonly body: { readonly ccc: 1; }; }; readonly step2: { ...; }; }; }[T][S]'.
.Firstly, I don't expect any errors in my type definition. Although it's possible of course that my mental model is wrong, or I don't understand some compiler limitations.
Secondly, the error message is quite obscure to me, it doesn't help to understand what I do wrong and how to fix it.
🙂 Expected behavior
I expect that the
EntityType
will be successfully constructed and will be something like this:In fact, it seems to me based on my "tests" in the playground above that the
EntityType
type construction works well, despite the error in the type definition.The text was updated successfully, but these errors were encountered: