Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!