[Typescript] 88. Hard - Simple Vue
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 contextthis
, but you won't be accessible to other computed values or methods. -
computed
is 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. -
methods
is an Object of functions that take the context asthis
as well. Methods can access the fields exposed bydata
,computed
as well as othermethods
. The different betweencomputed
is thatmethods
exposed 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())
}
}
})
/* _____________ Your Code Here _____________ */
type GetComputed<T> = {
[Key in keyof T]: T[Key] extends () => infer R ? R: never;
}
interface SimpleVueOptions<Data, Computed, Methods> {
data(): Data,
// Computed should able to access Data
computed: Computed & ThisType<Data & Computed>,
// Computed methods `fullname() {}` should be a prop `fullname: string` on Methods
methods: Methods & ThisType<Data & GetComputed<Computed> & Methods>
}
declare function SimpleVue<Data, Computed, Methods>(options: SimpleVueOptions<Data, Computed, Methods>): any
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
SimpleVue({
data(this: ThisType<{}>) {
// @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
},
},
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-11-08 [Kotlin] Mapping between two entities
2018-11-08 [Node.js] Trigger a File Download in Express
2018-11-08 [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack
2017-11-08 [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
2017-11-08 [PWA] Deal with caches in PWA
2016-11-08 [AngularJS] Using an AngularJS directive to hide the keyboard on submit
2016-11-08 [Javascript Natural] Break up language strings into parts using Natural