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

128
Visualizações
js: define options for function arguments

I have a question about Javascript/Typescript functions arguments and clean code. So I will use a simple example to illustrate my mean.

I created a React component with view property as props:

<Grid view="Horizontal" />

type PropsInterface = {
  view: string
}

const Grid = ( { view }:PropsInterface )=>{
    ...
}

view can have several options. For example: "Horizontal", "Vertical" etc.

Question: What is the best way to define view options so that I don't forget them or that other developers can easily understand? Is the bottom method good? Are there better ways?

type PropsInterface = {
  view: "Horizontal" | "Vertical"
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Typescript has an Enum type -

enum ViewDirection {
  Horizontal = "Horizontal",
  Down = "Vertical",
}

From TypeScript Docs:

In modern TypeScript, you may not need an enum when an object with as const could suffice:

So you can do:

const ViewDirection = {
  Horizontal = "Horizontal",
  Down = "Vertical",
} as const;

VS String Type

If the values of the strings are subject to change then using a string-enum means only changing 1 string literal, whereas using a string-type means changing them everywhere they're used.

In layman terms you can change Horizontal = "Horizontal" to Horizontal = "H" without the need to change it everywhere when using a string enum.

More in depth here:

Difference between string enums and string literal types in TS

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