英文:
Why is the overload signature not compatible with the implementation signature when an optional parameter is a string?
问题
以下是已翻译的内容:
我有以下函数及其重载:
```typescript
export function doAction(type: Type, mode: 'horizontal'): string;
export function doAction(type: Type, count2: number): string;
export function doAction(type: Type, count?: number, count2?: number, mode?: string): string {
return type;
}
然而,TS告诉我TS2394: This overload signature is not compatible with its implementation signature.
,问题出在mode
参数上。如果我将其类型更改为number
,它可以工作。我对此感到困惑,不明白为什么会发生这种情况。有什么想法吗?
我尝试了更改和交换参数的位置,使它们成为必需的,等等,只是为了看看它的行为如何。我原本期望它会正常工作,因为它只是一个基本的重载,并且其他简单的类型都可以正常工作。我不明白为什么它是一个字符串就会出问题。我甚至尝试输入mode: string
(也就是说,没有值,只有类型),它仍然不起作用。我查看了其他大约30个问题,但似乎没有涵盖这种情况的。
英文:
I have the following function and its overloads:
export function doAction(type: Type, mode: 'horizontal'): string;
export function doAction(type: Type, count2: number): string;
export function doAction(type: Type, count?: number, count2?: number, mode?: string): string {
return type;
}
However, TS is telling me that TS2394: This overload signature is not compatible with its implementation signature.
, the problem being the mode
parameter. If I change its type to number
, it works. I'm baffled as to why this is happening. Any ideas?
I've tried changing and swapping parameters around, making them required, etc., just to see how it'd behave. I was expecting it to just work, given it's just a basic overload, and the other, simple types just work. I don't see why it being a string creates issues. I even tried to input mode: string
(so, no value, just a type), and it still refuses to work.
I looked at like 30+ other questions, but none seem to cover this case.
答案1
得分: 0
以下是翻译好的部分:
"可选参数必须按顺序排列,名称不重要。
所以,如果在这里选择数字作为第二个参数(是否可选并不重要),它永远不会是一个字符串,因此永远不会与你的第一个重载兼容。同样,如果你在实现中选择了第二个参数作为字符串,它永远不会是一个数字,所以会导致第二个重载抛出错误。
就像 TypeScript 所说的,它与实现不兼容。
所以如果你可以修改你的实现如下:
export function doAction(type: Type, count?: unknown, count2?: number, mode?: string): string {
return type;
}
这是一个示例,'any' 或 'number | "horizontal" ' 也可以工作。
尽管如果你不知道它将是什么类型,对我来说更有意义的是将其类型设为 unknown,它会强制你正确检查你的代码。"
英文:
The optional arguments must be in order and the names don't matter.
So here, if you choose number as the second argument (it doesn't matter if it's optional), it will never be a string and therefore will never be compatible with your first overload. Similarly, if you had chosen your second argument as a string in your implementation, it would never be a number, so it's your second overload that would throw an error.
Like typescript say it's no compatible with the implementation.
So if you can modify your implementation like that:
export function doAction(type: Type, count?: unknown, count2?: number, mode?: string): string {
return type;
}
This is an example, 'any' or 'number | "horizontal" ' would also work.
Although it makes more sense to me to have it type unknown if you don't know what type it will be and it forces you to check your code properly.
答案2
得分: 0
你的实现期望第二个参数是一个 number
;你需要将其更改为接受 number | 'horizontal'
,以便两种重载都被接受:
export function doAction(type: Type, count?: number | 'horizontal', count2?: number, mode?: string): string {
return type;
}
英文:
Your implementation expects the second argument to be a number
; you need to change it to take number | 'horizontal'
, so that both overloads are expected:
export function doAction(type: Type, count?: number | 'horizontal', count2?: number, mode?: string): string {
return type;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论