首页 > 其他分享 >[Typescript] Deal with types of function overloads

[Typescript] Deal with types of function overloads

时间:2024-11-25 21:33:10浏览次数:9  
标签:function Typescript overloads never TReturn TOverload extends type

/**
 * Utility for extracting the parameters from a function overload (for typed emits)
 * https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709
 */
export type OverloadParameters<T extends (...args: any[]) => any> = Parameters<
  OverloadUnion<T>
>

type OverloadProps<TOverload> = Pick<TOverload, keyof TOverload>

type OverloadUnionRecursive<
  TOverload,
  TPartialOverload = unknown,
> = TOverload extends (...args: infer TArgs) => infer TReturn
  ? TPartialOverload extends TOverload
    ? never
    :
        | OverloadUnionRecursive<
            TPartialOverload & TOverload,
            TPartialOverload &
              ((...args: TArgs) => TReturn) &
              OverloadProps<TOverload>
          >
        | ((...args: TArgs) => TReturn)
  : never

type OverloadUnion<TOverload extends (...args: any[]) => any> = Exclude<
  OverloadUnionRecursive<(() => never) & TOverload>,
  TOverload extends () => never ? never : () => never
>

https://www.typescriptlang.org/play/?ts=4.7.2#code/C4TwDgpgBA8gbhATgGwPYEMAmAFRqwDOAPACrxJpYB8UAvFNgJYDGA1qeShpgDRSsQQqAGZQyCLtQDcAWABQoSLAmVMAVQB2jVBoBKEZgFdEBRgg4rufEtnSJgjdMk6q6UQxtYbUAdw016cQpuKAgAD2AIDUwCKAAKeSgoADpUuwBzAgAuKEYNYSQxAEFETPkASjoaPILEMX1gYw1EqAB+KAB6DoZECAQNYFz8vMZIqF6jE20NKAAjECgCYHwwPPTxg2NTHSgfAAsosVt7R2dLLBakrqg99Fj0ZiMAW0NkdEjMKCdkKBEoYAOYhcIVM6Q072MEAIyUuRzsDicwKwoQiURiQPOmFh7Q0fSQsKysKSSQAPspglhNNN9JNTOYicTGTZ4ackZ8AGQYim8BmMpLMk6IzFQTlxOKpZIZbLFUoESq0GgkBpNSqctm4fDEIKSTBUXnEvVyPnEsliiVSnIkEqZeWK5WIDTlFo5XESWQKcDQNlUnQWbkoyLRWLitKynLoDQgADaA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Z+V8u7sWfmfeiUhOjdA5qAj6kRb4t3vi3R++Da713IeJEBi1qGTjkB-Ae3QABy9N-K3DAoCamdQ-h9R1LEcaTx0AzjkbiT48twIeDeleQSrYIARnvF-Qc7xcjAAAOSxBAvtPCZ0jxHSUkfEgqA8YvSBuVJQZsWzgUkaoTy3QxGtnePjKifkURczAM1O81EnI0BnsNPYGi7YDhXutdenj-ihUBMOHQgxxz-T2oDZqVEAASvhtJ8FGMYhWTwwaDAIJAZgjBhAsHOvPXoRlrgjSEgVbRq8ewOCygYxYXY-ZG3QAUXIogVEqVCTJCQHw5gPFYMkhMSTraOKxtcNKtRMrrCUqonYfxcr5UKoURoC0oS-DqKVJo-wKqhO8D4KAoNwbmM2mnJQ6o7D7PxqQAMaJgzmjDAmaMcYrIRhAEmY47z9pEDzr6EgVB+4eheZie0GgSCek+WZb58RfmZHDJGWM8ZgVJiRSiyAkLD4qVILCz+W8M5LAAEI53oK8vKBUPlFxwSQ8uTCoDX25Vy1ucwH4d2IS6bSvc4XPO3pnWl+9GWIsKk0Yl2Y2VVw5RfHBrFmG8tofy9uDChWnxFW-bh4rdECLGOBCsM8NhlUuUoYmhjl7QDyOmTMtN9wfSPNAbZGhYjXSEjZJJsM6ZBxdcjUYakgA

标签:function,Typescript,overloads,never,TReturn,TOverload,extends,type
From: https://www.cnblogs.com/Answer1215/p/18568801

相关文章

  • Vue3+Typescript+Axios+.NetCore实现导出Excel文件功能
    前端代码//导出ExcelconstexportMaintenanceOrderSettlementItemExcelClick=async()=>{leturl=`${VITE_APP_API_URL}/api/app/maintenance/settlement-service-item/${currentMaintenanceOrderId.value}/${currentMaintenanceOrderSettlementRow.value.id}`;......
  • TypeScript很麻烦,不想使用! 转载
    原文链接:https://juejin.cn/post/7344282440725577765本文已经授权【稀土掘金技术社区】官方公众号独家原创发布。前言最近,我们部门在开发一个组件库时,我注意到一些团队成员对使用TypeScript表示出了抵触情绪,他们常常抱怨说:“TypeScript太麻烦了,我们不想用!”起初,我对此感到困......
  • Loss Function
    机器学习的本质目标函数:Log-likelihood对数似然函数$\logL(\theta)=\sum_{i=1}^n\logP(y|x,\theta)$其中,\(P(y|x,\theta)\):表示似然函数,给定特征\(x\)和参数\(\theta\),预测类别为\(y\)的概率\(P(y|x,\theta)\)由模型的modelhead原始输出logits,经过sigmoid......
  • [Vue] Use render function
    Sometimewhendoingthingisharderintemplatesyntax,youcanswitchtousingrenderfunctionintead.Forexample,wehaves Stackcomponent,itdynamicallywrappingchildelementwithadivandsomedefaultstylingapplied.<!DOCTYPEhtml><htm......
  • 我开发了许多智能家居设备,支持通过MQTT接入home Assitant ,我想用php开发一个网站,通过
    您好,您想开发一个PHP网站,通过OpenAI的API和FunctionCalling功能,实现智能家居的控制。这是一个非常有趣的项目,下面我将为您提供实现思路和步骤。1.整体架构思路用户界面(PHP网站):用户可以在网站上与AI进行聊天。OpenAIAPI交互:将用户的输入发送给OpenAI的API,使用Functio......
  • SAP-ABAP开发学习-FUNCTION ALV
    ALV概览        ALV全称SAPListView,是SAP提供的一个强大的数据报表显示工具。ALV实质上是一个屏幕控件对象,它通过程序传递数据内表的方式来显示数据。实现方式:调用标准函数;优化接口:用户可以实现对字段的排序、筛选及统计等功能。显示方式:List类似于write语句输......
  • Vue3 + TypeScript:从环境搭建到组件通信的完整前端开发教程
    在前端开发领域,Vue3与TypeScript的组合备受青睐。Vue3带来高效灵活的开发体验,TypeScript则提供强大的类型安全和可维护性。本文将详细介绍如何使用Vue3和TypeScript进行开发,文章内容将按照以下顺序展开:一、环境准备1.安装Node.js:从Node.js官方网站下载并安装适合......
  • 【题解】【记忆化递归】——Function
    【题解】【记忆化递归】——FunctionFunction题目描述输入格式输出格式输入输出样例输入#1输出#1提示数据规模与约定1.思路解析2.AC代码Function通往洛谷的传送门题目描述对于一个递归函数w......
  • TypeScript新手学习教程--接口
    TypeScript也支持接口,跟Java类似,这对于学习过java,c#,php语言的人更容易上手,虽然类似,但是也有不同,下面开始学习。1、 接口声明TypeScript的核心原则之一是对值所具有的结构进行类型检查。它有时被称做“鸭式辨型法”或“结构性子类型化”。在TypeScript里,接口的作用就是为......
  • TypeScript语法总结
    1.Typescript概述融合了后端面向对象思想的超级版的JavaScript语言。​TypeScript是JavaScript的超集,扩展了JavaScript的语法。特点:(1)TypeScript:【静态类型检查器】可在编译时检查错误(2)TypeScript为JS的超集(3)TS===JS+【类型】js有的ts都有(4)TS规范了JS......