Simple Vue Prompt
/* 6 - Simple Vue ------- by Anthony Fu (@antfu) #hard #this #application #vue ### Question 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, - `data` is a simple function that returns an object that exposes the context `this`, but you won't be accessible to other computed values or methods. - `computed` is an Object of functions that take the context as `this`, doing some calculation and returns the result. The computed results should be exposed to the context as the plain return values instead of functions. - `methods` is an Object of functions that take the context as `this` as well. Methods can access the fields exposed by `data`, `computed` as well as other `methods`. The different between `computed` is that `methods` exposed as functions as-is. The type of `SimpleVue`'s return value can be arbitrary. ```ts const instance = SimpleVue({ data() { return { firstname: 'Type', lastname: 'Challenges', amount: 10, } }, computed: { fullname() { return this.firstname + ' ' + this.lastname } }, methods: { hi() { alert(this.fullname.toLowerCase()) } } }) ``` > View on GitHub: https://tsch.js.org/6 */ /* _____________ Your Code Here _____________ */ declare function SimpleVue(options: any): any /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' SimpleVue({ data() { // @ts-expect-error this.firstname // @ts-expect-error this.getRandom() // @ts-expect-error this.data() return { firstname: 'Type', lastname: 'Challenges', amount: 10, } }, computed: { fullname() { return `${this.firstname} ${this.lastname}` }, }, methods: { getRandom() { return Math.random() }, hi() { alert(this.amount) alert(this.fullname.toLowerCase()) alert(this.getRandom()) }, test() { const fullname = this.fullname const cases: [Expect<Equal<typeof fullname, string>>] = [] as any }, }, }) /* _____________ Further Steps _____________ */ /* > Share your solutions: https://tsch.js.org/6/answer > View solutions: https://tsch.js.org/6/solutions > More Challenges: https://tsch.js.org */ Code of the type ONLY, no explanations.
fill the variables
This prompt has 5 variables. Pro fills them into a ready-to-paste prompt for you — no manual find-and-replace.
{hi() {
alert(this.fullname.toLowerCase())}{Equal, Expect}{fullname() {
return `${this.firstname}{this.lastname}{getRandom() {
return Math.random()}
Unlock with Pro →when to use it
Community prompt sourced from the open-source GitHub repo babelcloud/LLM-RGB (MIT). A "Simple Vue Prompt" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
roleplaycommunitygeneral
source
babelcloud/LLM-RGB · MIT