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

typescript use the interface to define prop and emit #4558

Closed
mioxs opened this issue Sep 11, 2021 · 3 comments
Closed

typescript use the interface to define prop and emit #4558

mioxs opened this issue Sep 11, 2021 · 3 comments

Comments

@mioxs
Copy link

mioxs commented Sep 11, 2021

What problem does this feature solve?

component.d.ts

import {
  DefineComponent,
  RenderFunction,
  SetupContext,
} from '@vue/runtime-core';

export declare type Emits<T> = {
  [K in keyof T as `on${Capitalize<K & string>}`]?: T[K];
};

export declare type KeyNames<T> = {
  [K in keyof T]: `${K & string}`;
}[keyof T];

export declare interface DefineComponentOption<P, E, R> {
  name?: string;
  props?: Array<KeyNames<P>> | Record<KeyNames<P>, any>;
  emits?: Array<KeyNames<E>> | Record<KeyNames<P>, any>;
  setup?: (
    props: Readonly<P>,
    ctx: SetupContext<Required<E>>,
  ) => R | RenderFunction;
}

export declare function defineComponent<P, E, R = Record<string, unknown>>(
  options: DefineComponentOption<P, E, R>,
): DefineComponent<P & Emits<E>, R>;

What does the proposed API look like?

example
Button.tsx

export interface ButtonProps  {
  label?: string;
}

export interface ButtonEmits {
  click: (value?: string) => void;
  close: () => void;
}

export  default defineComponent({
  name: 'Button',
  props: ['label'],
  emits: ['click', 'close'],
  setup(props: ButtonProps, { emit }: SetupContext<ButtonEmits>) {
    return () => (<></>);
  },
});

VButton.tsx

import { ButtonProps , ButtonEmits } form './Button';

export interface VButtonProps extends ButtonProps  {
  color?: string;
}

export interface VButtonEmits extends Omit<ButtonEmits, 'close'> {}

export default defineComponent({
  name: 'VButton',
  props: ['label','color'],
  emits: ['click'],
  setup(props: VButtonProps, { emit }: SetupContext<VButtonEmits>) {
    return () => (<></>);
  },
});
@edison1105
Copy link
Member

duplicate of #4294 ?

@mioxs
Copy link
Author

mioxs commented Sep 11, 2021

not script-setup

@posva
Copy link
Member

posva commented Sep 12, 2021

Duplicate of #4408

Please, take the time to explain what you are trying to achieve instead of just copy-pasting big chunks or code and letting maintainers try to guess.

@posva posva closed this as completed Sep 12, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants