-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Polymorphic this type does not work with conditional mapped types #32550
Comments
I'm not sure if this approach is possible because a key type may not constrain the type of values it maps to, it can only say that is a key of some type. (If someone does have a solution I'd be interested to see it!) Would this work for your use-case? public getNumber<K extends keyof this>(this: Record<K, number>, key: K): number {
return this[key];
} |
@jack-williams I cannot seem to get that to work. When constructing the class I get:
|
Can you post an extended example? Here is my version: playground |
@jack-williams I got your example working eventually. The problem is that I need access to other properties of The problem I'm trying to solve is a generalised, inheritable version of DataLoader priming. I can use |
We can tell from inspection that |
I also hit that today when was trying to make an amazing type definition, minimum example: type PickProperties<T, P> = Pick<T, {
[K in keyof T]: T[K] extends P ? K : never;
}[keyof T]>;
type Numbers<T> = PickProperties<T, number>;
type Identity<T extends number> = T;
const use = <T, K extends keyof Numbers<T>>(
service: K,
fn: Identity<Numbers<T>[K]>
): void => {} @RyanCavanaugh it's that hard to implement a constrain based on keyof? Because the constrain that edit, workaround found: // same code as above just append it
type IsNumbers<T> = T extends number ? T : never;
const use = <T, K extends keyof Numbers<T>, U extends IsNumbers<T[K]>>(
service: K,
fn: Identity<U>
): void => {} |
TypeScript Version: 3.5.3
Search Terms: Polymorphic this mapped conditional
Code
Expected behavior:
NumberedKeys<Example>
works fine, butNumberedKeys<this>
does not.Actual behavior:
Playground Link: link
Related Issues: n/a
The text was updated successfully, but these errors were encountered: