Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda