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

267
Visualizações
how to refer to a nested field in an object in Typescript

I want to write a type that is generated automatically depending on user data

type CleanPropType = typeof Array | typeof Object | typeof Function | typeof Boolean | typeof String;

type Prop = {
    type: CleanPropType,
    default?:any
}

// define the type of properties for all components

type Props = {
    [name:string]:Prop
}

// define properties for a specific component

let customComponentProps = {
    name:{
        type:String
    }
}

type customComponentGenerator<T extends Props> = {
    [name in keyof T]: ReturnType<T[name].type> // here error. WHY ?
}

// define the type of properties for a specific component
// but here an error occurs when accessing the "type" field 

type customComponent = Partial<customComponentGenerator<typeof customComponentProps>>;

// I need the resulting type to work correctly
let userComponentProps : customComponent = {
    name:'ComponentNameString',
} // ok

let userComponentProps2 : custromComponent = {
    name:false, // must be err
    otherField: 'str' // // must be err
} 

the main problem is that I cannot access the nested field 'type', though T extends Props. thx

link

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