jKraut
jKraut

Reputation: 2487

Typescript: Argument of type 'number' is not assignable to parameter of type 'string'

Trying to figure out a simple way to drop decimals of a number in TypeScript. Based on what I could find, the ParseInt method should work, but getting the error

Argument of type 'number' is not assignable to parameter of type 'string'

Code:

function testFunction (frame_count: number, frame_count_correct: number): number {
let score: number = 0 
if (frame_count > 1) {
    score = frame_count_correct/frame_count
} else {
    score = 0
}

return parseInt(score);

}

Upvotes: 3

Views: 3397

Answers (2)

Theodor Losev
Theodor Losev

Reputation: 392

You are trying to use parseInt function which is defined to string variables with double typed variable. Are you sure? You can use Math.floor(score) instead.

Upvotes: 1

Estus Flask
Estus Flask

Reputation: 222309

score is number. parseInt expects string argument because its purpose is to parse integers from strings, as function name suggests.

If the intention is to return integer part, parseInt isn't needed. Instead, it should be:

score = Math.floor(frame_count_correct/frame_count)

Upvotes: 2

Related Questions