I need help figuring out why TypeScript is not recognizing that props.user
is of type UserInterface
. Any advice or guidance would be greatly appreciated.
You can reach me at [email protected], [email protected], [email protected]. This seems to be more of a VueJS or TypeScript issue rather than a Quasar-related problem.
Here's the UserInterface for reference:
export default interface UserInterface {
id: number,
email: string,
name: string,
agent_id: string
}
Component:
<template>
<q-avatar :color="color" :text-color="textColor" :size="size" :title="user.name" style="outline: 2px solid #ffffff">
{{ initials(user.name) }}
</q-avatar>
</template>
<script lang="ts">
import UserInterface from 'logic/interfaces/UserInterface'
import {computed, defineComponent, PropType} from 'vue'
const colors: Record<string, string> = {
A: 'blue',
K: 'black',
R: 'purple',
S: 'primary'
}
export default defineComponent({
name: 'UserIcon',
props: {
user: {
type: Object as PropType<UserInterface>,
required: true
},
size: {
type: String,
required: false,
default: 'lg',
validator: function (value: string) {
return ['xs', 'sm', 'md', 'lg', 'xl'].indexOf(value) !== -1
}
},
textColor: {
type: String,
required: false,
default: 'white'
}
},
setup (props) {
const initial = props.user.agent_id.charAt(0)
const color = computed(() => {
return colors[initial] || 'green'
})
return {
color,
initials (name: string) {
const names = name.split(' ')
let initials = names[0].charAt(0)
if (names.length > 1) {
initials += names[names.length - 1].charAt(0)
}
return initials
}
}
}
})
</script>
The VueJS 3 documentation https://v3.vuejs.org/guide/typescript-support.html#using-with-composition-api states:
In the setup() function, you don't need to specify types for the props parameter as they will be inferred from the component options.
However, I keep receiving a compilation error and I'm unsure about what I might be overlooking.
Result:
Failed to compile.
TS2339: Property 'user' does not exist on type 'Readonly<LooseRequired<Readonly<{ [x: number]: string; } & { length?: number | undefined; toString?: string | undefined; toLocaleString?: string | undefined; concat?: string[] | undefined; join?: string | undefined; ... 15 more ...; includes?: ((searchElement: string, fromIndex?: number | undefined) => boolean) | un...'.
38 | },
39 | setup (props) {
> 40 | const initial = props.user.agent_id.charAt(0)
| ^^^^
41 | const color = computed(() => {
42 | return colors[initial] || 'green'
43 | })
Notes:
Adding a @ts-ignore
above the line in question temporarily resolves the error, but it doesn't address the underlying issue.
I've attempted deleting node_modules and restarting everything to rule out any glitches.
This code is running within a Docker image.