Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility types, ComponentProps and ComponentListeners #2094

Open
wonderful-panda opened this issue Sep 10, 2020 · 5 comments · May be fixed by #2179
Open

Add utility types, ComponentProps and ComponentListeners #2094

wonderful-panda opened this issue Sep 10, 2020 · 5 comments · May be fixed by #2179
Labels

Comments

@wonderful-panda
Copy link
Contributor

What problem does this feature solve?

Make it easy to write component in TypeScript.

What does the proposed API look like?

ComponentProps

Extract props type from component. Equivalent to React's ComponentProps.

const MyComp1 = defineComponent({
  props: {
    foo: { type: String, required: true },
    bar: Number
  }
})

const MyComp2 = defineComponent({
  props: ['foo', 'bar']
})

const MyComp3 = (props: { text: string }) => (<div>{props.text}</div>)

// { foo: string, bar?: number }
type MyComp1Type = ComponentProps<typeof MyComp1>

// { foo?: any, bar?: any }
type MyComp2Type = ComponentProps<typeof MyComp2>

// { text: string }
type MyComp3Type = ComponentProps<typeof MyComp3>

ComponentListeners

Extract event handlers type from component.

const MyComp1 = defineComponent({
  emits: {
    input: (value: string) => true,
    click: (x: number, y: number) => true
  }
})

const MyComp2 = defineComponent({
  emits: ['input', 'click']
})

// { input: (value: string) => void, click: (x: number, y: number) => void }
type MyComp1Listeners = ComponentListeners<typeof MyComp1>

// { input: (...args: any[]) => void, click: (...args: any[]) => void }
type MyComp2Listeners = ComponentListeners<typeof MyComp2>

Usecases

Writing wrapper component

const MyComp = defineComponent({
  props: { foo: String, bar: Number, baz: Boolean }
})

type MyCompProps = ComponentProps<typeof MyComp>

const WrappedMyComp = (props: Omit<MyCompProps, 'foo'>)=> h(MyComp, { foo: 'FooValue', ...props })

Writing event handlers safely

const MyComp = defineComponent({
  emits: {
    input: (value: string) => true,
    click: (x: number, y: number) => true
  }
})

type MyCompListeners = ComponentListeners<typeof MyComp>

const Parent = defineComponent({
  components: { MyComp },
  setup() {
    const onClick: MyCompListeners['click'] = (x, y) => {
       ...
    }
    const onInput: MyCompListeners['input'] = value => {
      ...
    }
    return { onClick, onInput }
  }
})

Improve JSX typing (by template literal types of TS4.1)

// transform { foo: T1, bar: T2 } to { onFoo?: T1, onBar?: T2 }
type On<T> = {
    [K in keyof T & string as `on${capitalize K}`]?: T[K]
}
declare global {
  namespace JSX {
   // Make enable to use onXxx as component attributes
    type LibraryManagedAttributes<C, P> = C extends new () => infer V
      ? (V extends { $props: infer P } ? P : {}) & On<ComponentListeners<C>>
      : P
  }
}
@yyx990803
Copy link
Member

Should be fairly straightforward to provide - open to PR.

@yyx990803 yyx990803 added the ✨ feature request New feature or request label Sep 14, 2020
wonderful-panda added a commit to wonderful-panda/vue-next that referenced this issue Sep 21, 2020
- ComponentProps: Extract props type of component
- ComponentListeners: Extract event handlers type of component
@LinusBorg
Copy link
Member

@wonderful-panda I see you started working on this in your fork. Do you need any help to get this ready for a PR?

@wonderful-panda
Copy link
Contributor Author

@LinusBorg PR is already opened.

#2179

@LinusBorg
Copy link
Member

Oh, I missed that. Will link issue and PR for better tracking now.

Thanks!

@bartenra
Copy link
Contributor

bartenra commented Apr 21, 2021

pikax helped me out on Discord with this snippet:

type Props<T> = T extends new () => { $props: infer P } ? P : never

I added a comment to your PR, I think it's a great addition but it should definitely be documented since these utility classes are meant for public consumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants