Vue2 with typescript, Property does not exist on type
Well, I don't have a good answer for you, but I have theory:
The original type declaration resides in vue/types/options.d.ts:
type DefaultData<V> = object | ((this: V) => object); // here is the kicker
Data=DefaultData<V>
data?: Data;
And I found that:
data():object { ... // this.snackbar does not exist
data(){ ... // this.snackbar does exist.
data(): any { ... // this.snackbar does exist.
data(): { snackbar: boolean; msg: string; color: string } { ... // Also valid
I think without your object declaration typescript will think that data
is DefaultData<V> = object
. But once you say it returns an object, data will suddenly match ((this: V) => object)
instead. Now typescript expects this
to be of type V
(which I assume is a vue-instance) and since that vue instance does not have a snackbar in it's definition, boom, typescript throws.
Lot's of guessing here, but I think pretty much anything except explicitly returning object
would work to not match that second signature in DefaultData<V>
.