microsoft / microsoft/TypeScript
`Parameters` returns `never` when used with a generic function that combines `Pick` and `Partial` on the generic type argument
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
🔎 Search Terms
partial pick generic parameters never
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Parameters
⏯ Playground Link
💻 Code
type Obj =
| { type: 'a', id: string, a: string }
| { type: 'b', id: string, b: number };
interface API {
works<T extends Obj>(
obj: T,
): void;
alsoWorks<T extends Obj>(
obj: Pick<T, 'type' | 'id'>,
): void;
alsoAlsoWorks<T extends Obj>(
obj: Partial<T>
): void;
doesntWork<T extends Obj>(
obj: Pick<T, 'type' | 'id'> & Partial<T>
): void;
}
type WorksParams = Parameters<API['works']>; // [obj: Obj]
type AlsoWorksParams = Parameters<API['alsoWorks']>; // [obj: Pick<Obj, "type" | "id">]
type AlsoAlsoWorksParams = Parameters<API['alsoAlsoWorks']>; // [obj: Partial<Obj>]
type DoesntWorkParams = Parameters<API['doesntWork']>; // never
🙁 Actual behavior
Parameters<API['doesntWork']> returns never
🙂 Expected behavior
The Parameters<API['doesntWork']> case behaves like the other cases and returns [obj: Pick<Obj, "type" | "id"> & Partial<Obj>].
Additional information about the issue
I'm not sure why Parameters works for all cases except when Pick and Partial are used together. I'm having the same problem when using extends and infer to get the parameters.
My use case is to prepend an argument event: IpcMainInvokeEvent to several generic functions to match the signature expected by the listener argument of Electron's ipcMain.handle method. If anyone knows of a workaround for this Parameters problem, I'd love to hear it.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 TypeScript Playground 开始,缩减涉及 Pick<T, 'type' | 'id'> & Partial 的 API 示例。比较四种方法的 Parameters 结果,并追踪相关的类型推断行为。当 Pick 和 Partial 的组合情况产生预期的参数元组,且其他情况没有回归时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100