soakw
soakw

Reputation: 11

Argument of type '{ test3: number; }' is not assignable to parameter of type '{ test1: any; test2: any; area: any; }'

export function ra({ test1, test2, test3 }) {
    if(test1) {
        return `test1`
    } else if(test2) {
        return `test2`
    }
    if(test3) {
        return `test3`
    }
}
console.log(ra({ test1: "a" }))

My error is Argument of type '{ test3: number; }' is not assignable to parameter of type '{ test1: any; test2: any; area: any; }'.

I'm doing this for a school project and I don't understand the error

Upvotes: 0

Views: 24

Answers (1)

Cody Duong
Cody Duong

Reputation: 2472

You are missing key/value pairs in the arguments Object, you have test1 but are missing test2 and test3

For example, this will work

console.log(ra({ test1: "a" , test2: "a", test3: "a"}))

If you did this on purpose, you have to explicitly type your parameters to allow optional key/value pairs with the ?. Otherwise TypeScript's default behavior is to expect they will be defined.

type ra2Parameters = {
  test1?: string;
  test2?: string;
  test3?: string;
}

export function ra2({ test1, test2, test3 }: ra2Parameters) {
    if(test1) {
        return `test1`
    } else if(test2) {
        return `test2`
    } 
    if (test3) {
        return `test3`
    }
}

//This will work now
console.log(ra2({ test1: "a" }))

View this on TypeScript Playground

Upvotes: 1

Related Questions