2.4 KiB
Simple Vue

by Anthony Fu @antfu
Implement a simpiled version of a Vue-like typing support.
By providing a function name SimpleVue (similar to Vue.extend or defineComponent), it should properly infer the this type inside computed and methods.
In this challenge, we assume that SimpleVue take an Object with data, computed and methods fields as it's only argument,
-
datais a simple function that returns a object that expose the the contextthis, -
computedis an Object of functions that take the context asthis, doing some calculation and returns the result. The computed results should be exposed to the context as the plain return values instead of functions. -
methodsis an Object of functions that take the context asthisas well. Methods can access the fields exposed bydata,computedas well as othermethods. The different betweencomputedis thatmethodsexposed as functions as-is.
The type of SimpleVue's return value can be arbitrary.
const instance = SimpleVue({
data() {
return {
firstname: 'Type',
lastname: 'Challenges',
amount: 10,
}
},
computed: {
fullname() {
return this.firstname + ' ' + this.lastname
}
},
methods: {
hi() {
alert(this.fullname.toLowerCase())
}
}
})