question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

[P in keyof T]: T[P] not accepting inferred base type via extends

See original GitHub issue

TypeScript 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:

https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgJIFkD2ATCAbZAbwCgBIYbALmQGcwpQBzAbmIF9jiwBPABxQCKAV2jcAPABUAfMgC8RMgG0ACslDIA1hG6YYyCQF0A-NQkqD7ThAAevTFDDIEeODRrIAQq4iTkNyCDY7hg4+DIkpLxCAEZ4wAjIMKDYABQAjiJQ3NTCopJSAJTUAG6YFAqkpAD0VcgAYvYA7nBQ2MhgABYoGaLtmMjYcGBw0d5kpAiYIDSYeBAAdHiYjOmZ3AWspBxkUbHxickA8iAQHtyoqRTUdAwgjEXIpeURpJ3ANPNJgSmEam1sGzIHA4QA

Related Issues:

Didnโ€™t find any related issue

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jonhuecommented, Jan 28, 2021

I submitted a fix with #42382, would really appreciate some feedback! ๐Ÿ˜„

1reaction
anlaurencommented, Dec 28, 2020

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:

type BaseType = {id: string};
function fun<M extends BaseType>(s: M["id"]): Partial<M> {
  return { id: s }; // Type '{ id: M["id"]; }' is not assignable to type 'Partial<M>'.
}


Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found