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

114
Visualizações
Typescript - Create a function where argument validates object based on passed in component

I am looking for a way to have a function take two arguments - a react functional component, and the correct prop for that component. The catch is that any component can be passed in and therefore any component could have its own interface. My goal is that the second argument is type aware based on the first arg.

What I have so far.

interface ComponentAProps { hello: string }
interface ComponentBProps { world: string }
interface someFunction{ (component: React.FC<any>, props: ???) => void }

// Two components as an example.
const ComponentA = ({ hello }: ComponentAProps) => return <p>hello</p>
const ComponentB = ({ world }: ComponentAProps) => return <p>world</p>


// This function takes in the component, and an object representing the props
const someFunction: someFunction = (ComponentA, { hello: 'hello' }) => void
const someFunction: someFunction = (ComponentB, { world: 'world' }) => void


// This should give a type error
const someFunction: someFunction = (ComponentB, { hello: 'hello' }) => console.log(props)

I believe I could do something like

props: <ComponentAProps | ComponentBProps>

but the issue is there could be a Component C,D,E...Z components.

Essentially this function should validate that the 2nd argument passed in are the props of whatever component is in arg 1

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To solve the core issue of making the second arg validate the props of the component we can use typescript Genrics

Instead of Validating the props based on the component passed in, we can validate the props by telling the function what props to expect. How it would implement in the OP code

interface ComponentAProps { hello: string }
interface ComponentBProps { world: string }

// This says says that a type can be passed through to the second arg
function someFunction<Type>(component: React.FC<any>, props: Type) {
    return void
}

// Two components as an example.
const ComponentA = ({ hello }: ComponentAProps) => return <p>hello</p>
const ComponentB = ({ world }: ComponentAProps) => return <p>world</p>


// These will not produce type Error
someFunction<ComponentAProps>(ComponentA, { hello: 'hello' }) 
someFunction<ComponentBProps>(ComponentB, { world: 'world' }) 


// This will give a type error because we are telling the function
// that the 2 arg needs to be of type ComponentBProps
someFunction<ComponentBProps>(ComponentA, { hello: 'hello' })
about 4 years ago · Juan Pablo Isaza Relatório

0

At runtime, there is no type information. What you can do is create some kind of schema for your objects, and then validate them against that schema. One of the most popular packages for that is ajv

about 4 years ago · Juan Pablo Isaza Relatório
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