Modify Parameters Utility in case of union/intersection of function types

未关闭
#57,074 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
compilers

调研方向

首先查看 issue 中的 union、intersection 和 overload 示例,包括当前和提议的 Parameters 结果。完成的标准是:这些情况的行为(包括 union 或 intersection 对象类型上的方法)符合提议的语义,同时不改变运行时行为。

由索引模型根据 Issue 内容生成。

描述

Awaiting More Feedback Suggestion
🔍 Search Terms

Parameters Utility with union of functions
Parameters Utility with intersection of functions

✅ Viability Checklist
⭐ Suggestion

This proposal suggests revising the Parameters utility type.

There are multiple sub-proposals which may be considered independently. The sub-proposals are:

Case 1: The function is a union of function types
// Case 1:
type POr = Parameters< ((x:1|2)=>void) | ((x:2|3)=>void) >;
// Current: [x: 1 | 2] | [x: 2 | 3]
// Proposed: [x: 1 | 2] & [x: 2 | 3]
//  with reduction -> [x: 2]

There is no way to call the function with a value of type 1 or 3 without risking a runtime error. The only safe value is 2. The proposed type reflects this. This agrees with existing TypeScript inference behavior.

declare const f:((x:1|2)=>void) | ((x:2|3)=>void);
f(1); // error;

Therefore the proposed definition is:

Parameters<T|U> = Parameters<T> & Parameters<U>

Because there is no way to generate an & type using a distributed conditional generic type (i.e., the way it currently written, Parameters would have to become an intrinsic type function.

Case 2.x The function type is an intersection or overload of function types

In these cases also Parameters would have to become an intrinsic type function.

Case 2.1 The function type is an intersection of function types
// Case 2.1
type PAnd = Parameters< ((x:1|2)=>void) & ((x:2|3)=>void) >;
// PAnd = [x: 2 | 3]
// Proposed: [x: 1 | 2] | [x: 2 | 3]

Currently TypeScript treats an intersection like an overload list, and then picks the last overload under the assumption that it is the "most permissive catch-all case". This assumption is not always true. When it is not true, Parameters is not useful.

The proposed type could be used as the parameter signature for the implementation of the function intersection.

Case 2.2.1 The function type is an overload type without the catch-all case
// Case 2.2.1
type POverloadNoCover2 = Parameters< {
    (x:1|2):void;
    (x:2|3):void;
}>;
// POverloadNoCover2 = [x: 2 | 3]
// Proposed: [x: 1 | 2] | [x: 2 | 3]

Therefore the proposed definition is:

Parameters<T&U> = Parameters<T> | Parameters<U>

Currently TypeScript picks the last overload under the assumption that it is the "most permissive catch-all case". This assumption is not always true. When it is not true, Parameters is not useful.

The proposed type could be used as the parameter signature of implementation of the function intersection.

Case 2.2.2 The function type is an overload type with the catch-all case
// Case 2.2.2
type POverloaWithCover2 = Parameters< {
    (x:1|2):void;
    (x:2|3):void;
    (x:1|2|3):void;
}>;
// POverloaWitnCover2 = [x: 1 | 2 | 3]
// Proposed: [x: 1 | 2] | [x: 2 | 3] | [x: 1 | 2 | 3]

In this case the proposed type and the current type are the same, and may serve as the parameter signature of implementation of the function intersection.

Methods of unioned/intersected Object types

Methods of unioned/intersected Object types fall within the cases described above.

interface X {
    p: X;
    f(x:X):void;
};
interface Y{
    p: Y;
    f(x:Y):void;
};

// Case 1:
type P1 = Parameters<(X|Y)["f"]>;
// P1 = [x: X] | [x: Y]
// Proposed: [x: X] & [x: Y]

// Case 2.1:
type P2 = Parameters<(X&Y)["f"]>;
// P2 = [x: Y]
// Proposed: [x: X] | [x: Y]
📃 Motivating Example

As shown above.

💻 Use Cases
  • It is currently impossible to use Parameters to get the correct type of a union of functions.
  • It is currently impossible to use Parameters to get the implementation signature type of an intersection of functions. (Although there are cases where it works, that is not guaranteed.)
  • It is currently impossible to use Parameters to get the implementation signature type of a function overload. (Although there are cases where it works, that is not guaranteed.)
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。