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

165
Views
tipo genérico "local" en un tipo mapeado mecanografiado

Estoy usando tipos mapeados mecanografiados para intentar transformar los campos de un tipo/clase en una función que toma un argumento donde el tipo difiere para cada campo y se determina en función de la función asignada al campo:

Básicamente quiero mapear un tipo como:

 type ArgsSchema<Args> = Args extends string ? "string" : Args extends number ? "number" : Args extends Date ? typeof Date : never type From = { field: string, anotherField: boolean } // Map it to type To = { field: { resolve: (args: number) => string, argsSchema: ArgsSchema<number> }, anotherField: { resolve: (args: Date) => boolean argsSchema: ArgsSchema<Date> } } // For an object like const to = { field: { resolve: (args: number) => `Hello number ${args}`, argsSchema: "string" }, anotherField: { resolve: (args: Date) => args.getSeconds() > 10, argsSchema: Date } }

Intenté lo siguiente, pero falla porque intenta inferir el tipo Args a un solo tipo, no a un tipo por campo.

 type MapTest<Args> = { [Key in keyof From]: { resolve: (args: Args) => From[Key], argsSchema: ArgsSchema<Args> } } function map<Args>(mt: MapTest<Args>): void { } map({ field: { resolve: (args: number) => "hello", argsSchema: "number" }, anotherField: { resolve: (args: string) => true, argsSchema: "string" } })

También probé una variable de tipo en la función en sí, pero tiene un error que dice que no es asignable.

 type MapTest2 = { [Key in keyof From]: { resolve: <Args> (args: Args) => From[Key], argsSchema: ArgsSchema<Args> } } function map2(mt: MapTest2): void { } map2({ field: { resolve: (args: number) => "hello", argsSchema: "number" }, anotherField: { resolve: (args: string) => true, argsSchema: "string" } })
about 4 years ago · Juan Pablo Isaza
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!