3

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...

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.