Skip to content

213 - Vue Basic Props #215

Open
Open

Description

@antfu
type InferComputed<C extends Record<string, any>> = { [K in keyof C]: ReturnType<C[K]> }

type Prop<T = any> = PropType<T> | { type?: PropType<T> }
type PropType<T> = PropConstructor<T> | PropConstructor<T>[]

type PropConstructor<T = any> =
  | { new (...args: any[]): T & object }
  | { (): T }

type InferPropType<P> = 
  P extends Prop<infer T>
    ? unknown extends T
      ? any
      : T
    : any 

type InferProps<P extends Record<string, any>> = 
  { [K in keyof P]: InferPropType<P[K]> }

declare function VueBasicProps<P, D, C extends Record<string, any>, M, Props = InferProps<P>>(
  options: {
    props?: P,
    data(this: Props): D,
    computed: C & ThisType<Props & D & InferComputed<C> & M>,
    methods: M & ThisType<Props & D & InferComputed<C> & M>
  }
): Props & D & InferComputed<C> & M

Basically, to get Boolean (which is an instance of BooleanConstructor) to the type boolean, you just infer the new () => T function to get its instance type. This works with custom classes as well. There is also a built-in type utils InstanceType that serves the same purpose.

If you want to challenge your self further, you can try to read the real types for Vue 3 here.

Activity

  1. added
    answer
    en
    on Sep 15, 2020
  2. xianshenglu commented on Feb 26, 2021

    @xianshenglu
    Contributor

    Well, thanks for the great solution. And it takes several hours to get it.
    However, based on my test, to address this

    get Boolean (which is an instance of BooleanConstructor) to the type boolean

    The above code solves this by (): T not new () => T. Considering the following code

    type PropConstructor1<T = any> =  { new(...args: any[]): (T & object) }
    type PropConstructor2<T = any> =  { (): T }
    type PropConstructor3<T = any> = { new(...args: any[]): (T & object) } | { (): T }
    
    type e1 = StringConstructor extends PropConstructor1<infer R> ? R : any // String
    type e2 = StringConstructor extends PropConstructor2<infer R> ? R : any // string
    type e3 = StringConstructor extends PropConstructor3<infer R> ? R : any // string

    So, the original PropConstructor works by { (): T }.

  3. xianshenglu commented on Feb 26, 2021

    @xianshenglu
    Contributor

    However, another question appears

    type PropConstructor4<T = any> =  { new(...args: any[]): (T ) }|{ (): T }
    type e4 = StringConstructor extends PropConstructor4<infer R> ? R : any // String

    Compared with

    type PropConstructor3<T = any> =  { new(...args: any[]): (T & object) }|{ (): T }
    type e3 = StringConstructor extends PropConstructor3<infer R> ? R : any // string

    I don't understand how does & object works? Even using & 1 also works. Any explanations? @antfu

    Also posted in stackoverflow

  4. xianshenglu commented on Mar 3, 2021

    @xianshenglu
    Contributor

    Also, related issues microsoft/TypeScript#32389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    213answeren

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions