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

119
Visualizações
Type Array is not assignable to number

I am trying to get the highest number from 5 dice. I call pips as eyes in this code. Here's my code:

// get the highest count
  // https://stackoverflow.com/questions/63098129/typescript-get-max-value-from-a-mixed-array
  getHighestNumber(dice: Die[]): number {
    return dice.reduce(
      (a: number, b: any, i: number, arr: any[]) =>
        arr.filter(x => x.eyes === a).length >=
        arr.filter(x => x.eyes === b.eyes).length ? a : b.eyes, null);
  }

The errors I'm getting:

Type 'Die' is not assignable to type 'number'.ts(2322)
No overload matches this call.
  Overload 1 of 3, '(callbackfn: (previousValue: Die, currentValue: Die, currentIndex: number, array: Die[]) => Die, initialValue: Die): Die', gave the following error.
    Argument of type '(a: number, b: any, i: number, arr: any[]) => any' is not assignable to parameter of type '(previousValue: Die, currentValue: Die, currentIndex: number, array: Die[]) => Die'.
      Types of parameters 'a' and 'previousValue' are incompatible.
        Type 'Die' is not assignable to type 'number'.
  Overload 2 of 3, '(callbackfn: (previousValue: number, currentValue: Die, currentIndex: number, array: Die[]) => number, initialValue: number): number', gave the following error.
    Argument of type 'null' is not assignable to parameter of type 'number'.ts(2769)

How do you fix this primitive logic? It's for a dice game called Yathzee and I'm developing it using the latest stable TypeScript version and newest stable Angular version.

I've tried the quick fix, which for some reason adds an underscore for i, didn't help, setting the type for b didn't help either...

My die data model looks like this:

export interface Die {
    eyes: number;
    locked: boolean;
}
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