打字稿中的重载函数类型

     2023-03-31     238

关键词:

【中文标题】打字稿中的重载函数类型【英文标题】:Overloaded function type in typescript 【发布时间】:2019-02-07 11:45:49 【问题描述】:

如何在不提供具体函数的情况下创建重载的函数类型? 通过检查重载函数的类型,似乎可以在接口/对象类型上使用多个调用签名:

function a(input: string): string
function a(input: number): number
function a(input: string | number): string | number 
  return input


type A = typeof a

type B = 
  (input: string): string
  (input: number): number


const b: B = a // Okay!

用联合类型定义相同的想法(没有那种令人讨厌的包罗万象的情况,你需要让重载快乐)也有效,类型在两个方向上都兼容!

type C = ((input: number) => number) & ((input: string) => string)

const c: C = b // Okay!

const a2: A = c // Okay too!

但是我现在如何制作适合这种类型的函数呢?我是否也必须使用重载?

const x: A = (input: string | number) => input

const y: A = (input: string | number) => 
  if (typeof input === "number") return input
  if (typeof input === "string") return input
  throw "excrement"

两者都失败并出现完全相同的以下错误:

Type '(input: string | number) => string | number' is not assignable to type ' (input: string): string; (input: number): number; '.
  Type 'string | number' is not assignable to type 'string'.
    Type 'number' is not assignable to type 'string'.

最糟糕的是,即使我使用可读性较差的联合类型C,也会发生这种情况

Type '(input: string | number) => string | number' is not assignable to type 'C'.
  Type '(input: string | number) => string | number' is not assignable to type '(input: number) => number'.
    Type 'string | number' is not assignable to type 'number'.
      Type 'string' is not assignable to type 'number'.

希望我做错了什么,并且有一个简单的解决方法。 否则,当我需要要求在某处传递的函数处理具有相应返回类型的多个调用签名时,我最好的选择是什么?

【问题讨论】:

【参考方案1】:

您可以使用generic 声明解决此问题:

type Input = string | number

function a<T extends Input>(input: T): T 
  return input


type A = typeof a

type B = 
  (input: string): string
  (input: number): number


const b: B = a // Okay!

type C = ((input: number) => number) & ((input: string) => string)

const c: C = b // Okay!

const a2: A = c // Okay too!

至于xy,您不能松散地定义参数类型并期望严格推断输出类型。如果您要将xy 声明为A 类型,请省略定义函数的输入类型:

const x: A = input => input

const y: A = input => 
  if (typeof input === "number") return input
  if (typeof input === "string") return input
  throw "excr"

您可以在TypeScript Playground demo 验证上述所有内容。

【讨论】:

非常抱歉,x 函数中有错字。正确的 x (const x: A = (input: Input) =&gt; input) 和 y 仍然失败,现在在这种情况下使用 Type '(input: Input) =&gt; Input' is not assignable to type '&lt;T extends Input&gt;(input: T) =&gt; T'. Type 'Input' is not assignable to type 'T'. Type 'string' is not assignable to type 'T'. 这里:typescriptlang.org/play/… 这是因为您在将 xy 函数声明为更严格的类型时定义的输入太弱。请参阅我的更新答案。 这确实修复了通用示例。但是我如何输入一个不是恒等函数的函数呢?比如返回相反的类型? 示例:(B 接受,正文没有)typescriptlang.org/play/…【参考方案2】:

要定义具有多个调用签名的函数,如果您无法编写一个可分配给您想要的所有调用签名的单个调用签名,您将不得不使用任一重载(它具有更宽松的兼容性规则带有实现签名的调用签名)或类型断言。你没有错过任何东西。

【讨论】:

但是我如何使用匿名函数来实现这种类型呢? 如果你想使用匿名函数而不是本地重载函数定义,你必须使用类型断言。 :'( keystrokes keystrokes

获取泛型函数的类型而不调用打字稿中的函数

】获取泛型函数的类型而不调用打字稿中的函数【英文标题】:Getthetypeofgenericfunctionwithoutinvokingthefunctionintypescript【发布时间】:2019-05-2923:22:20【问题描述】:我有一个这样的通用函数:functionfoo<T>(val:T)returnval;我有一些类型... 查看详情

如何在打字稿中为 const 定义重载签名?

】如何在打字稿中为const定义重载签名?【英文标题】:Howtodefineoverloadedsignaturesforaconstintypescript?【发布时间】:2020-08-0510:27:10【问题描述】:我们可以在typescript中定义这样的重载函数:functionhello(name:number):void;functionhello(name:stri... 查看详情

打字稿中的泛型函数找不到接口属性

】打字稿中的泛型函数找不到接口属性【英文标题】:Genericsfunctionintypescriptcan\'tfindinterfaceproperty【发布时间】:2017-05-2913:26:33【问题描述】:我正在尝试使用泛型在打字稿中创建以下函数,但它向我显示以下错误:“\'CustomerInte... 查看详情

如何将方法的泛型类型限制为打字稿中的对象?

】如何将方法的泛型类型限制为打字稿中的对象?【英文标题】:Howtorestrictagenerictypeofamethodtoanobjectintypescript?【发布时间】:2021-11-2503:15:32【问题描述】:我一直在尝试为我的方法提出一个泛型类型,并将该类型限制为具有以下... 查看详情

无法让嵌套类型保护与打字稿中的联合类型一起使用

】无法让嵌套类型保护与打字稿中的联合类型一起使用【英文标题】:Unabletogetnestedtypeguardstoworkwithuniontypesintypescript【发布时间】:2019-10-1910:17:10【问题描述】:我正在尝试通过删除任何虚假/错误值来清理数据,然后再将其传递... 查看详情

打字稿中的“三个点”是啥意思?

】打字稿中的“三个点”是啥意思?【英文标题】:Whatdoesthe"threedots"intypescripttypemean?打字稿中的“三个点”是什么意思?【发布时间】:2019-10-2413:16:52【问题描述】:我今天在Typescript中遇到了一些问题。这个类型中的三... 查看详情

打字稿中的通用对象类型

】打字稿中的通用对象类型【英文标题】:GenericObjecttypeintypescript【发布时间】:2017-03-3104:43:15【问题描述】:在打字稿中是否有任何方法可以为变量分配通用对象类型。这就是我所说的“通用对象类型”letmyVariable:GenericObject=1//... 查看详情

对象打字稿中的条件类型

】对象打字稿中的条件类型【英文标题】:ConditionalTypeinObjectTypescript【发布时间】:2021-11-0612:25:25【问题描述】:typeOperationType=\'Insert\'|\'Update\'|\'Delete\';exporttypeBaseModel<TextendsDocument>=operationType:OperationType,properties:"U 查看详情

扩展打字稿中的快速请求类型

】扩展打字稿中的快速请求类型【英文标题】:extendsexpressrequesttypeintypescript【发布时间】:2020-03-2415:21:20【问题描述】:重复:ExtendExpressRequestobjectusingTypescriptimportRouterfrom\'express\';import*asuuidfrom\'uuid\';我正在使用打字稿3.6.4。我... 查看详情

打字稿中的全局类型

】打字稿中的全局类型【英文标题】:Globaltypesintypescript【发布时间】:2017-08-1612:42:22【问题描述】:有没有办法在你的typescript文件中创建一个定义全局可访问类型的文件?我喜欢打字稿,但发现当我想要真正的类型安全时,我... 查看详情

通用类型(T)与打字稿中的任何类型有啥区别

】通用类型(T)与打字稿中的任何类型有啥区别【英文标题】:WhatarethedifferencebetweengenericType(T)vsanyintypescript通用类型(T)与打字稿中的任何类型有什么区别【发布时间】:2017-10-1619:26:59【问题描述】:打字稿中genericType(T)与any... 查看详情

打字稿中的多个构造函数

】打字稿中的多个构造函数【英文标题】:Multipleconstructorsintypescript【发布时间】:2022-01-2318:32:49【问题描述】:如何在typescript中定义多个构造函数?例如,我想有以下代码:classFolderextendsAssetconstructor(repositoryId:string,assetId:string... 查看详情

避免任何类型在打字稿中获取枚举值

】避免任何类型在打字稿中获取枚举值【英文标题】:Avoidanytypegettingenumvaluesintypescript【发布时间】:2021-07-0822:47:24【问题描述】:我需要遍历枚举类型以填充反应组件中的一些选项。在枚举下方找到从中返回键和值的函数。expo... 查看详情

如何从打字稿中的标记联合类型中提取类型?

】如何从打字稿中的标记联合类型中提取类型?【英文标题】:Howtoextractatypefromataggeduniontypeintypescript?【发布时间】:2019-03-2708:10:43【问题描述】:假设已经有一个类型定义如下:exporttypeItem=type:\'text\',content:string|type:\'link\',url:st... 查看详情

打字稿中的方法返回类型为null

】打字稿中的方法返回类型为null【英文标题】:Methodreturntypewithnullintypescript【发布时间】:2019-12-0604:18:16【问题描述】:我的问题很简短,我会举个例子。我有一些课:classApublicproperty:Property;constructor(data:any)this.property=this.parsePr... 查看详情

如何覆盖打字稿中的属性?

】如何覆盖打字稿中的属性?【英文标题】:HowdoIoverrideapropertyintypescript?【发布时间】:2016-10-0823:15:27【问题描述】:由于当前的语言限制,这可能是不可能的,但我使用的是最新的TS(1.8.10),并且遇到了ui-grid类型的问题。IGridOp... 查看详情

如何理解打字稿中的“属性'名称'在'用户'类型中是私有的”

】如何理解打字稿中的“属性\\\'名称\\\'在\\\'用户\\\'类型中是私有的”【英文标题】:Howtounderstand"Property\'name\'isprivateintype\'User\'"intypescript如何理解打字稿中的“属性\'名称\'在\'用户\'类型中是私有的”【发布时间】:202... 查看详情

用玩笑测试打字稿中的私有函数

】用玩笑测试打字稿中的私有函数【英文标题】:testingprivatefunctionsintypescriptwithjest【发布时间】:2019-09-2609:46:50【问题描述】:在下面的代码中,我的测试用例按预期通过了,但我使用stryker进行突变测试,handleError函数在突变... 查看详情