Microsoft/TypeScript

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

Open

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

在 GitHub 查看
 (16 留言) (4 反應) (0 負責人)TypeScript (6,726 fork)batch import
Effort: DifficultExperience EnhancementHelp WantedSuggestion

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 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

貢獻者指南