In the process of developing a Vue3 app using Typescript, one of the components is designed to receive data through props
. Initially, everything functioned smoothly with the basic setup:
props: {
when: String,
data: Object
},
However, I decided to enhance the props
by incorporating default values, validators, and more:
props: {
when: {
type: String,
default: '',
validator(value: string) {
return ['today', 'tomorrow', 'later'].includes(value)
},
required: true
},
data: {
type: Object,
default() {
return {}
},
required: true
},
},
Subsequently, TypeScript started flagging issues within the setup()
function that are puzzling to me:
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)'.
pertaining to 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) =>...'.
related to the code:
if (props.when === 'today') {
// ...
}
There are additional errors, but resolving them seems feasible once I identify the root cause.