[Typescript] 30. Medium - Append Argument

For given function type Fn, and any type A (any in this context means we don't restrict the type, and I don't have in mind any type 😉) create a generic type which will take Fn as the first argument, A as the second, and will produce function type G which will be the same as Fn but with appended argument A as a last one.

For example,

type Fn = (a: number, b: string) => number

type Result = AppendArgument<Fn, boolean> 
// expected be (a: number, b: string, x: boolean) => number

 

/* _____________ Your Code Here _____________ */

type AppendArgument<Fn, A> = Fn extends (...args: infer Args) => infer RT ? (...args: [...Args, A]) => RT: never


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type Case1 = AppendArgument<(a: number, b: string) => number, boolean>
type Result1 = (a: number, b: string, x: boolean) => number

type Case2 = AppendArgument<() => void, undefined>
type Result2 = (x: undefined) => void

type cases = [
  Expect<Equal<Case1, Result1>>,
  Expect<Equal<Case2, Result2>>,
]

 

posted @   Zhentiw  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-09-13 [React] Handle Recoil Asynchronous Selectors using Loadables in React
2020-09-13 [Docker] Create a Docker configuration for a NestJS API
2020-09-13 [Javascript] Joi for validation
2020-09-13 [Machine Learning] Unrolling Parameters
2019-09-13 [React] Add client-side validation to your React forms with Yup
2018-09-13 [TypeScript] Define Custom Type Guard Functions in TypeScript
2018-09-13 [Sqlite3] Sqlite Introduction
点击右上角即可分享
微信分享提示