I am building a simple Vue3 app (using Typescript) and one of the components receives its data via props
. With the basic version everything works fine:
props: {
when: String,
data: Object
},
I then wanted to extend props
to add default values, validators, etc.
props: {
when: {
type: String,
default: '',
validator(value: string) {
return ['today', 'tomorrow', 'later'].includes(value)
},
required: true
},
data: {
type: Object,
default() {
return {}
},
required: true
},
},
And then TS started to raise issues that I do not understand (all in setup()
):
TS2345: Argument of type '"data"' is not assignable to parameter of type 'string & ((number | unique symbol | "length" | "toString" | "toLocaleString" | "concat" | "join" | "slice" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | ... 29 more ... | ((searchElement: never, fromIndex?: number | undefined) => boolean)) & string)'.
for the line
let allEvents = toRef(props, 'data')
TS2339: Property 'when' does not exist on type 'Readonly<LooseRequired<Readonly<readonly unknown[] & { [x: number]: string; } & { [iterator]?: IterableIterator<string> | undefined; length?: number | undefined; toString?: string | undefined; toLocaleString?: string | undefined; ... 17 more ...; includes?: ((searchElement: string, fromIndex?: number | undefined) =>...'.
for the line
if (props.when === 'today') {
// ...
}
There are a few more but I believe that I will fix them once I understand where the core of the problem is.