Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Define type to array behavior

I need to crate a type that can hold an array where each element is a function that gets the previous output as an input. For example, a valid array is:

const pipe: SpecialArray = [
    () => string,
    (prev:string) => number,
    (prev:number) => boolean
];

An invalid array would be:

const pipe: SpecialArray = [
    () => string,
    (prev:number) => number, // <-- Here number should be string
    (prev:number) => boolean
];

Is there a way to define such structure in TypeScript?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I believe no, since Array is homogenous, you'll have to define in the form [(a: string) => number, (b: number) => boolean], and you can't really enumerate all (unless a code generator is used).

about 4 years ago · Juan Pablo Isaza Report

0

It is impossible in TypeScript to create standalone type for such kind of array. In order to do that you should create compose function, infer each function from arguments and validate it. Please see my article and questions/answers on SO: first , second, third.

Also you can check this compose function in fnts lib

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!