Skip to content

Instantly share code, notes, and snippets.

@vezaynk
Created July 13, 2022 03:50
Show Gist options
  • Save vezaynk/9fa29c434d234620c7dbcd9d0a811f95 to your computer and use it in GitHub Desktop.
Save vezaynk/9fa29c434d234620c7dbcd9d0a811f95 to your computer and use it in GitHub Desktop.
Solution to Simple Vue by Anthony Fu (@antfu) from typescript-challenge
type ToGetter<T> = {
[P in keyof T]: T[P] extends (...args: any) => any ? ReturnType<T[P]> : never;
}
interface VueOptions<D, C, M> {
data: () => D;
computed: C;
methods: M;
}
declare function SimpleVue<
D, C, M,
ComputedProperties = ToGetter<C>,
BoundThis = D & ComputedProperties & M & { data: () => D}
>(options:
VueOptions<D, C, M>
&
ThisType<BoundThis>): any
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
SimpleVue({
data() {
this.firstname
this.getRandom()
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
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment