首页 > 其他分享 >[Typescript] 88. Hard - Simple Vue

[Typescript] 88. Hard - Simple Vue

时间:2022-11-08 02:55:05浏览次数:44  
标签:Typescript return computed Simple SimpleVue 88 fullname data methods

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 datacomputed 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 datacomputed 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.

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
    },
  },
})

 

标签:Typescript,return,computed,Simple,SimpleVue,88,fullname,data,methods
From: https://www.cnblogs.com/Answer1215/p/16868036.html

相关文章

  • [Typescript] ThisType
    Thisutilitydoesnotreturnatransformedtype.Instead,itservesasamarkerforacontextual this type.Notethatthe noImplicitThis flagmustbeenabl......
  • Typescript类型体操 - Join
    题目中文实现类型版本的Array.join,Join<T,U>接受数组T和string或者number类型U作为泛型参数,并返回U连接数组T后的字符串.EnglishImplementthetyp......
  • A simple usage of "Gobuster"
    Atoolfor enumeratingallwebpageofawebsite.=====> https://github.com/OJ/gobusterItrequeiresawordlisttomatchthenamesofthepagesinaparticu......
  • React中常见的TypeScript定义实战
    一引沿Fiber架构是React16中引入的新概念,目的就是解决大型React应用卡顿,React在遍历更新每一个节点的时候都不是用的真实DOM,都是采用虚拟DOM,所以可以理解成fiber就是R......
  • React-hooks+TypeScript最佳实战
    ReactHooks什么是HooksReact一直都提倡使用函数组件,但是有时候需要使用state或者其他一些功能时,只能使用类组件,因为函数组件没有实例,没有生命周期函数,只有类组件才......
  • P5588 小猪佩奇爬树
    感谢所有AC传送门思路    画图得到四种情况,无色,单色,多色同链(两端点),多色不同链(多端点)。代码#include<iostream>#definemaxn1000100usingnamespacestd;ty......
  • Simple Math Problem
    ​​传送门​​思路:题目中给出的矩阵均为16进制表示,根据规律输出对应10进制数即可。#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintinf=0x3......
  • 常用类.SimpleDateFormat
    package常用类.calendar;importjava.text.SimpleDateFormat;importjava.util.Date;publicclasssimpleDateFormat{publicstaticvoidmain(String[]args)throw......
  • P8813(CSP-J2022T1)题解
    题意:算a ^ b,如果结果超出1e9就输出-1,反之输出结果。思路:边算边判加特判。代码:#include<cstdio>#definelllonglong#definemx1e9//边界usingnamespacestd;i......
  • P8814(CSP-J2022T2)题解
    题意:给定一个正整数 k,有k 次询问,每次给定三个正整数 n, e, d,求两个正整数 p, q,使 n = p × q且e × d =(p −1)×(q −1)+1。思路:通过题意可以发......