-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Better resolution for correlations in template literal types #57388
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
The union type you're getting is correct. The way the function is typed, the following is a valid call, which would return getKey<Type>(Type.TypeA, "c"); Technically, all of |
Same deal as #57372. |
I understand that |
One workaround (probably not the best) is to use mapped types: const getKey = <T extends Type = Type>(type: T, keys: KeyMap[T]) => `${type}.${keys}` as keyof {[K in Type as `${K}.${KeyMap[K]}`]:null}; Another is to use a seemingly useless const getKey = (<T extends Type = Type,>(type: T, keys: KeyMap[T]) => `${type}.${keys}` as (T extends any ? `${T}.${KeyMap[T]}`: never)) |
Thanks @rotu ! At least this does the job. Your second example is wild. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
"template literal types", "correlation"
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "template literal types"
β― Playground Link
https://www.typescriptlang.org/play?#code/KYOwrgtgBAKgngB2FA3gKCrRwCCUC8UARAC7Y5EA0GWSAQgcWfUWgL5prPIDSwcAWQCGCRukwBteEgB003AF0AXMSFEoAH2IAjIgG4aU7HOx1lxAMbqtRACb72BtBYD2IAM4koAc2Ak+cIwAPDBQwAAeJKC27rTIhPIAfAAU3CowlFAA1vzuKgHCCFIKAJQEiVAABgAkKNxsMrU5cO5slVBCsa4eJE7cUAAKLu7uAJbaADbAAbGEAEp+YABOIPJB3C4AZj5+ASHYiYkGQA
π» Code
π Actual behavior
type PossibleKeys = "typeA.a" | "typeA.b" | "typeA.c" | "typeA.d" | "typeB.a" | "typeB.b" | "typeB.c" | "typeB.d"
π Expected behavior
type PossibleKeys = "typeA.a" | "typeA.b" | "typeB.c" | "typeB.d"
Additional information about the issue
I know that the docs about Template Literal Types state that "For each interpolated position in the template literal, the unions are cross multiplied". But I am wondering whether we could to better in such cases or if there is an alternative way to implement this.
The text was updated successfully, but these errors were encountered: