You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exporttypeParam=number|string|boolean|Date|null;exporttypeParamFields<T>={[PinkeyofRequired<T>]: NonNullable<T[P]>extendsParam ? P : never;}[keyofT];interfaceIHasId{id: number;}classHasIdimplementsIHasId{id: number=1;}functionhasField<T>(x: T,field: ParamFields<T>){returnx[field];}hasField({x: 5},"x");functiontemplateHasId<TextendsIHasId>(x: T){returnhasField(x,"id");// Doesn't realize id has to be a field of x.}
Expected behavior:
hasId(x, "id"); compiles because it'd declared to have a field of x thats of type number.
Actual behavior:
Compains because it can't deduce "id" is a keyof T even though the interface says it is.
While it's true from a higher-ordering reasoning perspective that "id" would be assignable to any instantiated ParamFields<T> if T extends IHasId, that isn't the operation that happens here. TypeScript has to first produce a type for ParamFields<T> and then see if "id" is assignable to it, but ParamFields<T> is a higher-order operation that can't be resolved to a concrete type because it depends on a free type parameter T - the constraint here is not enough to change this fact here, because it would be unsound for ParamFields<T (extends IHasId)> to just be "id" because T could be instantiated with some type where the correct answer is more like "id" | "name", which is a wider type. As long as the type remains higher-order in a complex way like this, TS isn't able to make determinations about whether or not "id" is always going to be in the resulting type.
Probably your example is more complex in reality, but I would just make templateHasId non-generic since there's no reason for it to be generic.
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
generic deduction interface
Code
Expected behavior:
hasId(x, "id"); compiles because it'd declared to have a field of x thats of type number.
Actual behavior:
Compains because it can't deduce "id" is a keyof T even though the interface says it is.
Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=26&pc=1#code/KYDwDg9gTgLgBDAnmYcAKBDKGC2cC8cAdgK44BGwUcAPnAM4xQCWRA5rXORBADbAYinACIYYqOqV68A3ACg5oSLATJUmbDgBizYLwAm9ADwAVAHwE4Abzlw4AbTRxWcANbBEEAGZwASsABHEmYoYH1TMwBdAC44ADkIIjiSaQxyflNHSItQcSJDdCxcOAB+dDhYomAANyp5AF97d08fE0j5OVZxKC8MAGNUAEkACQx6Qf1rWztnfUqySigGhT7eMfo4UfHJ5hwwfhxgIhgNkbGJqZnZ+YoqSwBGZbkvEiI+mGZEuAALMZ09cLmAAUIFiJgANHAvLoDLENLh-gZjOYAJSXGahGAkKBCED2aEA9pyeoKOS-eiI-RAqxwUFwACscHqkIARCAWSiOi83h8vuI9mtxFsJqY4LkjgUzts4BYQWC0TYrpjsUJyZSQazmPoOTI4AB6PVwYQQYD0IgAcngoQwvGYAC9UFqfmMEBAuKgMFCYZNvLSAHTEhRAA
Related Issues:
#19211
#7294
The text was updated successfully, but these errors were encountered: