Skip to content

Commit 82a564b

Browse files
committed
changes
1 parent a1ed484 commit 82a564b

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

src/api/utility-types.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,20 +272,18 @@ extendAsHTMLElement(
272272

273273
You don't necessarily need to you `DeclareComponent`, but you can.
274274

275-
> Is necessary to use `new<...>{ $props: { ... } }` for Generic components.
275+
> Is necessary to use `new<...>{ $props: { ... } }` for Generic components, using type helpers with Generic params might break generic types.
276276

277277
```ts
278278
type ErrorLevel = 'debug' | 'warning' | 'error'
279279

280280
declare function ErrorComponent<T>(options: T): T &
281-
DeclareComponent<{
282281
new <T extends ErrorLevel = 'debug'>(): {
283282
$props: { type: T } & {
284283
[K in `on${Capitalize<T>}`]: (msg: string) => void
285284
}
286285
$slots: SlotsType<Record<T, (msg: string) => any[]>>
287286
}
288-
}>
289287

290288
const Comp = ErrorComponent(
291289
defineComponent({
@@ -415,3 +413,70 @@ Returns the component bindings from `data` and `setup`.
415413
```ts
416414

417415
```
416+
417+
## DefineComponentOptions & DefineComponentFromOptions {#definecomponentoptions}
418+
419+
Allows to get vue component options and generate `DefineComponent` type.
420+
421+
:::warning
422+
The generic order is not prone to change, but it might, `Options` should be the last generic used, new generics will be added before `Options`.
423+
:::
424+
425+
:::info
426+
Note that you if you need to handle generics, it should be it's own overload, since
427+
any change to the Generic definition might break the Generic type.
428+
:::
429+
430+
```ts
431+
declare function defineComponentWithDefaults<
432+
Props = never,
433+
RawBindings = {},
434+
D = {},
435+
C extends ComputedOptions = {},
436+
M extends MethodOptions = {},
437+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
438+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
439+
E extends EmitsOptions = {},
440+
EE extends string = string,
441+
I extends ComponentInjectOptions = {},
442+
II extends string = string,
443+
S extends SlotsType = {},
444+
Options extends {} = {}
445+
>(
446+
options: DefineComponentOptions<
447+
Props,
448+
RawBindings,
449+
D,
450+
C,
451+
M,
452+
Mixin,
453+
Extends,
454+
E,
455+
EE,
456+
I,
457+
II,
458+
S,
459+
Options
460+
>,
461+
defaults: Partial<
462+
[Props] extends [string]
463+
? { [K in Props]?: any }
464+
: ExtractPropTypes<Props>
465+
>
466+
): DefineComponentFromOptions<
467+
undefined extends Props ? {} : Props,
468+
RawBindings,
469+
D,
470+
C,
471+
M,
472+
Mixin,
473+
Extends,
474+
E,
475+
EE,
476+
I,
477+
II,
478+
S,
479+
Options
480+
>
481+
```
482+

src/guide/typescript/advanced.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ If you check the [defineComponent source-code](https://github.com/vuejs/core/blo
137137

138138
Returned from [defineComponent](./../../api/general.html#definecomponent),
139139
```
140+
141+
142+
### Bespoke `defineComponent`
143+
144+
Vue options are very hard to get the type hard, sometimes `defineComponent` is not enough and creating a bespoke `defineComponent` can be useful for some more advance cases, such as [@vue/test-utils](https://test-utils.vuejs.org/) [mount](https://test-utils.vuejs.org/api/#mount) method.
145+
146+
147+
http://localhost:5173/api/utility-types.html#definecomponentoptions

0 commit comments

Comments
 (0)