Skip to content

Commit

Permalink
fix(programmatic): make options interface extandable (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek authored Nov 19, 2024
1 parent 16cf988 commit 37e17f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export type ProgrammaticOptions<C extends VNodeTypes> = {
/** public options interface for programmatically called components */
export type PublicProgrammaticComponentOptions = EmitsToProps<
Pick<ProgrammaticComponentEmits, "close">
>;
> &
// make the type extendable
Record<string, any>;

/** useProgrammatic composable `open` function return value */
export type ProgrammaticExpose = ProgrammaticComponentExpose;
Expand Down
25 changes: 4 additions & 21 deletions packages/oruga/src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Component } from "vue";
import type { ComponentProps } from "vue-component-type-helpers";
import type { Component, EmitsToProps } from "vue";
import type { ComponentEmit } from "vue-component-type-helpers";

export type ClassBind = {
[x: string]: boolean;
Expand All @@ -21,25 +21,8 @@ export type DynamicComponent = string | object | CallableFunction | Component;
/** Define a property as required */
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };

/**
* Extract all properties of an object which starts with a `on` prefix.
* It also cuts the `on` prefix and lowercase the first char.
*/
type ComponentEmitsByProps<T> = {
[K in keyof T as K extends `on${infer S}`
? S extends `${infer First}${infer Rest}`
? `${Lowercase<First>}${Rest}`
: S
: never]: T[K];
};

/**
* Custom type helper which extracts the `$emits` type of an component as object.
*
* Because the `ComponentEmit` from "vue-component-type-helpers" does not export the emits as an object but as:
* `((event: "close", ...args: any[]) => void) | undefined`
*/
export type ComponentEmits<C> = ComponentEmitsByProps<ComponentProps<C>>;
/** Custom type helper which extracts the `$emits` type of an component and converts it to an props object. */
export type ComponentEmits<C> = EmitsToProps<ComponentEmit<C>>;

/** Extracts the type of property `K` in object `T`. */
type TypeOfKey<T, K extends string> = K extends keyof T ? T[K] : unknown;
Expand Down

0 comments on commit 37e17f5

Please sign in to comment.