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

贡献者指南