[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>>,
]
分类:
TypeScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源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