[P in keyof T]: T[P] not accepting inferred base type via extends
See original GitHub issueTypeScript Version: 3.8.3
Search Terms:
[P in keyof T]?: T[P]
typescript generic extends in keyof
typescript generic extends T[P]
typescript mongo collection generic
Code
interface IModel {
id: string;
}
type Query<T> = {
[P in keyof T]?: T[P]
}
export class Base<T extends IModel> {
public find(query: Query<T>): void {
// Forward the query to database
console.log(query);
}
public findOneById(id: string): void {
this.find({ id });
}
}
Expected behavior:
Itโs expected to allow to execute the find with the properties defined on IModel
as base of any other model;
Actual behavior:
It shows an error:
Argument of type '{ id: string; }' is not assignable to parameter of type 'Query<T>'.ts(2345)
Playground Link:
Related Issues:
Didnโt find any related issue
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:16 (7 by maintainers)
Top Results From Across the Web
How to interpret [p in keyof T] and T[p] in these TypeScript ...
Basically, the syntax [p in keyof T] means just that; p is one of the keys of the object T . Then, the...
Read more >Documentation - TypeScript 2.1
Enter Index Type Query or keyof ; An indexed type query keyof T yields the type of permitted property names for T ....
Read more >Overview - TypeScript
The Iterator type now allows users to specify the yielded type, the returned type, and the type that next can accept. interface Iterator<T,...
Read more >Announcing TypeScript 2.8 - Microsoft Developer Blogs
If you can't wait any longer, you can download TypeScript via NuGet ... Conditional types also provide us with a way to infer...
Read more >TypeScript Cheatsheet, Common Errors, and More
If an object has no declared index signature, TS can't know the return type when accessing by a key, so any is inferred...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I submitted a fix with #42382, would really appreciate some feedback! ๐
I was running in the same problem before i understood the concept of more restrictive subtypes. So, iโve tried to use the type from the generic itslef,
T["foo"]
in the above example but still no luck there. Should it be reported as a bug or am I getting something wrong: