benjimarshal22
benjimarshal22

Reputation: 109

Typescript error on reduce array function - "string' is not assignable to parameter of type

I am currently learning typescript and have run into a typescript error from the following function:

export interface WebspaceBackupArtifact {
    readonly date?: string;
    readonly name?: string | null;
    readonly path?: string | null;
    readonly type?: string | null;
}
 
interface WebspaceBackupArtifactExtended extends WebspaceBackupArtifact {
  size: string;
  type: string;
}

const selected = [] as WebspaceBackupArtifactExtended[],

const totalSize = (): number => {
return selected
        .filter(
          (artifact: WebspaceBackupArtifactExtended) =>
            artifact.type !== 'directory',
        )
        .reduce(
          (accumulator: number, artifact: WebspaceBackupArtifactExtended) =>
            accumulator + artifact.size,
          0,
        );
}

The above function produces the following errors:

ERROR in /app/src/views/myfile.vue(394,11): No overload matches this call. Overload 1 of 3, '(callbackfn: (previousValue: WebspaceBackupArtifactExtended, currentValue: WebspaceBackupArtifactExtended, currentIndex: number, array: WebspaceBackupArtifactExtended[]) => WebspaceBackupArtifactExtended, initialValue: WebspaceBackupArtifactExtended): WebspaceBackupArtifactExtended', gave the following error. my-project | Argument of type '(accumulator: number, artifact: WebspaceBackupArtifactExtended) => string' is not assignable to parameter of type '(previousValue: WebspaceBackupArtifactExtended, currentValue: WebspaceBackupArtifactExtended, currentIndex: number, array: WebspaceBackupArtifactExtended[]) => WebspaceBackupArtifactExtended'. my-project | Types of parameters 'accumulator' and 'previousValue' are incompatible. my-project | Type 'WebspaceBackupArtifactExtended' is not assignable to type 'number'.

and

my-project | Overload 2 of 3, '(callbackfn: (previousValue: number, currentValue: WebspaceBackupArtifactExtended, currentIndex: number, array: WebspaceBackupArtifactExtended[]) => number, initialValue: number): number', gave the following error. my-project
| Argument of type '(accumulator: number, artifact: WebspaceBackupArtifactExtended) => string' is not assignable to parameter of type '(previousValue: number, currentValue: WebspaceBackupArtifactExtended, currentIndex: number, array: WebspaceBackupArtifactExtended[]) => number'. my-project |
Type 'string' is not assignable to type 'number'. my-project |
392 | ) my-project | 393 | .reduce( my-project | > 394 | (accumulator: number, artifact: WebspaceBackupArtifactExtended) => my-project | |
^ my-project | 395 | accumulator + artifact.size, my-project | 396 | 0, my-project | 397 |
);

Why would it be throwing these errors after I have defined the types? I tried changing this line:

(accumulator: number, artifact: WebspaceBackupArtifactExtended) =>

to

(previousValue: WebspaceBackupArtifactExtended, currentValue: WebspaceBackupArtifactExtended) =>

but it still resulted in the same error message...

Upvotes: 3

Views: 1538

Answers (1)

Zoltan Magyar
Zoltan Magyar

Reputation: 873

You define WebspaceBackupArtifactExtended.size as number therefore accumulator + artifact.size returns a string. Changing the definition of size to number fixes the error.

Upvotes: 1

Related Questions