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

298
Visualizações
TypeScript: How to use Array.includes with possibly undefined value (optional chaining) on array of strings

Consider this example:

type SomeType = {
  paymentData?: {
    paymentMethod?: string;
  }
}

const someObj: SomeType = {};

const someTest = ['creditCard', 'payPal'].includes(someObj.paymentData?.paymentMethod);

This doesn't work in TypeScript because it infers the type of the array elements to be string so it requires the value used in with the includes function to also be of type string, but someObj.paymentData?.paymentMethod is possibly undefined aka the type is string | undefined. I cannot use the non-null assertion operator (!) as it can't be used after optional chaining.

JavaScript-wise it's perfectly fine if the includes checks with undefined, just TypeScript is unhappy about it. What are nice ways to satisfy TypeScript here?

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

0

One way is to cast the array to an array of string | undefined. This is probably my preferred way as it doesn't leave any traces in the transpiled JavaScript code. Also it still prevents you from accidentally checking against e.g. a number which actually doesn't make sense.

const someTest = (['creditCard', 'payPal'] as (string | undefined)[]).includes(someObj.paymentData?.paymentMethod);

It is a bit verbose in the TypeScript code tho, so there are two more options:

Checking upfront if someObj.paymentData?.paymentMethod is defined:

const someTest = !!someObj.paymentData?.paymentMethod && ['creditCard', 'payPal'].includes(someObj.paymentData.paymentMethod);

Ensuring the value is always a string by falling back to an empty string in case of undefined using the nullish coalescing operator (??):

const someTest = ['creditCard', 'payPal'].includes(someObj.paymentData?.paymentMethod ?? '');

For further reference, here is a related thread on GitHub abut the typing of the Array.includes function: https://github.com/microsoft/TypeScript/issues/26255

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Non-null assertion operator - !:

type SomeType = {
  paymentMethod?: string;
}

const someObj: SomeType = {};

const someTest = ['creditCard', 'payPal'].includes(someObj.paymentMethod!);

Working example

about 4 years ago · Juan Pablo Isaza Relatório

0

Ideally, the paymentMethod type would be defined. Usually this is defined by an API, if not, you should define it:

type PaymentMethod = 'creditCard' | 'payPal';

interface SomeType {
  paymentMethod?: PaymentMethod;
}

const someObj: SomeType = {};

const someTest = ['creditCard', 'payPal'].includes(someObj.paymentMethod);
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