Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
"local" generic type in a typescript mapped type

I'm using typescript mapped types to try and transform the fields of a type/class to a function that takes an argument where the type differs for each field and is determined base on the function assigned to the field:

Basically I want to map a type like:

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
  }
}

I've tried the following, but it fails because it tries to infer the Args type to a single type, not a per field type.

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" 
  }
})

I've also tried a type variable on the function itself, but it has an error saying it that it's not assignable.

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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda