TS为什么允许我在重载的函数声明中返回错误的类型?

huangapple go评论68阅读模式
英文:

Why does TS let me return wrong type in overloaded function declarations?

问题

我认为问题已经足够清晰,以下是让我感到困惑的代码部分:

```typescript
type T = {
  (value: number): number
  (value: string): string
}

第一个示例

const bar: T = (value: any) => {
  if (typeof value === "number") return []
  return value
}
bar(10) // []

对我来说,令人困惑的是编译器在以下示例中引发错误:

第二个示例

const bar: T = (value: any) => {
  return []
}

为什么我可以在第一个示例中返回[],但在第二个示例中返回[]时会出错?

谢谢!


<details>
<summary>英文:</summary>

I think the question is clear enough and here is the code that makes me wonder:

```typescript
type T = {
  (value: number): number
  (value: string): string
}

First Example

const bar: T = (value: any) =&gt; {
  if (typeof value === &quot;number&quot;) return []
  return value
}
bar(10) // []

For me it is inconsistent that the compiler then throws an error in the following example:

Second Example

const bar: T = (value: any) =&gt; {
  return []
}

Why can I return [] in the first example but get an error if I return [] in the second example?

Thank you!

答案1

得分: 3

any opts out of type checking. By using it, you're effectively telling Typescript to not care about type checking for the code paths that use it.

当你使用 any 时,它会禁用类型检查。通过使用它,你实际上在告诉 Typescript 不要关心使用它的代码路径的类型检查。

When you return an any type (as you are with value), Typescript will skip checking if it matches the declared return type. However, when you return an explicit empty array (as in your second example), that is a known type, not an any, so Typescript can check if it matches the declared return type.

当你返回一个 any 类型(就像你在 value 中所做的那样),Typescript 将跳过检查是否与声明的返回类型匹配。然而,当你返回一个显式的空数组(就像你的第二个示例中那样),那是一个已知的类型,而不是 any,因此Typescript可以检查它是否与声明的返回类型匹配。

英文:

any opts out of type checking. By using it, you're effectively telling Typescript to not care about type checking for the code paths that use it.

When you return an any type (as you are with value), Typescript will skip checking if it matches the declared return type. However, when you return an explicit empty array (as in your second example), that is a known type, not an any, so Typescript can check if it matches the declared return type.

答案2

得分: 1

如果函数的返回类型未指定(就像上面的示例一样),TypeScript 将通过创建可能返回值类型的联合来推断返回类型。

所以我们有 []any 的联合。将 any 类型与任何其他类型联合将导致结果为 any

type Result = any | []; // any

现在我们的函数签名解析为 (value: any) => any,因此可以将其分配给类型为 T 的变量。

这与重载无关。

英文:

If return type of the function is not specified (like in example above), typescript will infer the return type by creating union of possible return values' types.

So we have union of [] and any. Union of any type with whatever other type will result in any

type Result = any | []; // any

Playground

Now our function's signature resolved as (value: any) =&gt; any therefore assignment to variable of type T is allowed.

* This has nothing to do with overloads.

huangapple
  • 本文由 发表于 2020年1月7日 01:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定