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

212
Visualizações
How to set a functions parameter type to be a property KEY of ONE of a type's properties? Is this even possible?

How does one set a function's parameter type to accept a key of one of a type's properties? Is this even possible?

type A = {
    ['prop1']: string;
    ['props2']: boolean;
}
// arg would be either 'prop1' or 'prop2'
const example = (arg: property in A ) => {}

My assumption is that this is not even possible. If so, is there a way to get around this?

Is there a way to declare a type similar to how you can declare an object with an enum? Obviously, this is an object below, but could you do something similar with a type?

Example:

enum Enum {
    bold = 'bold',
    italic = 'italic',
}
const objWithEnumProperties: {[key in Enum]: any} = {
    [Enum.bold]: '',
    [Enum.italic]: ''
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

How to set a functions parameter type to be a property KEY of ONE of a types properties? Is this even possible?

Yes, using TypeScript keyof:

type Foo = { bar: string; baz: boolean; }

// arg is 'bar' | 'baz'
const fn = (arg: keyof Foo) => {}

Is there a way to declare a type similar to how you can declare a object with an enum?

You are probably wanting a const object assertion or an interface:

// Usable at runtime
const Foo = {
  bar: 'bar',
  baz: 'baz'
} as const;

// Only for typing
interface Foo {
  bar: string; 
  baz: boolean;
}
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