Microsoft/TypeScript

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

開放

#37,670 建立於 2020年3月29日

 (16 則留言) (4 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
Effort: DifficultExperience EnhancementHelp WantedSuggestion

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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

貢獻者指南