2

How do I resolve the following typescript error? For context, I am using the Vue 3 Composition API where I ultimately am using the result to specify whether or not a default option value should be <option ... selected v-if="!isMatch">.

Object is of type 'unknown'.

The error is highlighting the second 'option'.

props:{
    value: {
        required: true,
        type: String,
    },
    options: {
        required: true,
        type: Array,
    },
}
setup(props){
    const isMatch = () => props.options.find(option => {
        return option['code'] === props.value
    })
    return { isMatch }
}

Example 'Options' Data

[
    {
        "code": "CA",
        "name": "Canada"
    },
    {
        "code": "US",
        "name": "United States"
    }
]
4
  • Could you share the props setup part? By the looks of how you are using it on the template, I think RoToRa is right, you might want to use Array.prototype.some instead of find. Need more context here. Commented Jul 8, 2021 at 12:33
  • @YomT. Props added. Commented Jul 8, 2021 at 17:29
  • use computed for isMatch Commented Jul 8, 2021 at 17:52
  • @Lynx Thanks, but receiving the same error, unfortunately. Commented Jul 8, 2021 at 18:22

2 Answers 2

3

Following a bit of refactoring, I came up with this via the Vue Docs. I required the addition of two interface definitions and the PropType import.

import { defineComponent, PropType } from 'vue'

interface Option {
    code: String,
    name: String
}

interface Options extends Array<Option>{}

export default defineComponent({
    props: {
        id: {
            required: true,
            type: String,
        },
        title: {
            required: true,
            type: String,
        },
        selections: {
            required: true,
            type: Array as PropType<Options>
        }
        modelValue: {
            required: true,
            type: String,
        },
    },
    setup(props) {
        const isMatch = () =>
            props.selections.some(
                selection => selection['code'] === props.modelValue
            )

        return { isMatch }
    },
})
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, it's a common gotcha.
1

.find() returns an matching object from the array, but you seem to just want a boolean saying if such an object exists, so you should be using .some() instead.

BTW, you can simplify the arrow function by just using an expression instead of using return:

const isMatch = () => props.options.some(option => option['code'] === props.value)

2 Comments

Thanks, though I'm still needing to resolve the "Object is of type 'Unknown'" error.
Sorry, I thought that would help. I don't have any other ideas.

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.