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

WIP: Support for vue type helpers changes #2242

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
updated
pikax committed Nov 18, 2023
commit c7242ef8a275cadddc2e9aef04bb788da8f8f9e1
151 changes: 109 additions & 42 deletions patches/@vue__runtime-core@3.3.8.patch

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/baseWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { textContent } from './utils'
import type { TriggerOptions } from './createDomEvent'
import {
ComponentDefineOptions,
DefineComponentOptions,
DefineComponentFromOptions,
ComponentInjectOptions,
ComponentInstance,
ComponentInternalInstance,
@@ -131,7 +132,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
S extends SlotsType = {},
Options = {}
>(
selector: ComponentDefineOptions<
selector: DefineComponentOptions<
Props,
RawBindings,
D,
@@ -149,7 +150,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
): VueWrapper<
Props extends DefinedComponent
? ComponentInstance<Props>
: CreateComponentPublicInstance<
: DefineComponentFromOptions<
Props,
RawBindings,
D,
@@ -158,10 +159,9 @@ export default abstract class BaseWrapper<ElementType extends Node>
Mixin,
Extends,
E,
Props,
{},
true,
EE,
I,
II,
S,
Options
>
127 changes: 108 additions & 19 deletions src/mount.ts
Original file line number Diff line number Diff line change
@@ -3,13 +3,9 @@ import {
DefineComponent,
VNode,
ComponentInstance,
ComputedOptions,
MethodOptions,
ComponentOptionsMixin,
EmitsOptions,
ComponentInjectOptions,
SlotsType,
ComponentDefineOptions
ComponentObjectPropsOptions,
ExtractPropTypes
} from 'vue'
import type { ComponentSlots } from 'vue-component-type-helpers'
import { createInstance } from './createInstance'
@@ -108,24 +104,117 @@ export type ComponentMountingOptions<T, P> = Omit<
// >
// ): { props: Props }
// defineComponent

// TODO import from vue
export type ResolveProps<Props, E extends EmitsOptions> = Readonly<
([Props] extends [string]
? { [key in Props]?: any }
: [Props] extends [ComponentObjectPropsOptions]
? ExtractPropTypes<Props>
: Props extends never[]
? {}
: Props) &
({} extends E ? {} : {})
>

// export function mount<Component>(
// component: Component &
// DefineComponent<
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any,
// any
// >,
// options?: ComponentMountingOptions<
// Component,
// ComponentPropsWithDefaultOptional<Component>
// >
// ): VueWrapper<ComponentInstance<Component>> & {
// LOL: Component
// LOOL: ComponentPropsWithDefaultOptional<Component>
// }

export function mount<
T extends DefineComponent<
PropsOrOptions,
any,
any,
any,
any,
any,
any,
any,
any,
any
>,
PropsOrOptions extends object
T extends DefineComponent<any, any, any, any, any, any, any, any, any, any>
>(
originalComponent: T,
options?: ComponentMountingOptions<T, ComponentPropsWithDefaultOptional<T>>
): VueWrapper<ComponentInstance<T>>

// export function mount<
// Props = {},
// RawBindings = {},
// D = {},
// C extends ComputedOptions = {},
// M extends MethodOptions = {},
// Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
// Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
// E extends EmitsOptions = {},
// EE extends string = string,
// I extends ComponentInjectOptions = {},
// II extends string = string,
// S extends SlotsType = {},
// Options = {},
// Component extends DefineComponentOptions<
// Props,
// RawBindings,
// D,
// C,
// M,
// Mixin,
// Extends,
// E,
// EE,
// I,
// II,
// S,
// Options
// > = DefineComponentOptions<
// Props,
// RawBindings,
// D,
// C,
// M,
// Mixin,
// Extends,
// E,
// EE,
// I,
// II,
// S,
// Options
// >
// >(
// componentOptions: DefineComponentOptions<
// Props,
// RawBindings,
// D,
// C,
// M,
// Mixin,
// Extends,
// E,
// EE,
// I,
// II,
// S,
// Options
// >,
// options?: ComponentMountingOptions<
// Component,
// ComponentPropsWithDefaultOptional<Component>
// >
// ): VueWrapper<ComponentInstance<Props>>

// implementation
export function mount(
inputComponent: any,
6 changes: 3 additions & 3 deletions test-dts/mount.d-test.ts
Original file line number Diff line number Diff line change
@@ -85,15 +85,15 @@ mount(AppWithProps, {

expectError(
mount(AppWithProps, {
// @ts-expect-error wrong prop type should not compile
// @ts-expect-error wrong prop type should not compile
props: { a: 2 }
})
)

const AppWithArrayProps = {
props: ['a'],
props: ['a'] as ['a'],
template: ''
} as const
}

// accept props - vm is properly typed
expectType<string>(
4 changes: 2 additions & 2 deletions test-dts/shallowMount.d-test.ts
Original file line number Diff line number Diff line change
@@ -65,9 +65,9 @@ expectError(
)

const AppWithArrayProps = {
props: ['a'],
props: ['a'] as ['a'],
template: ''
} as const
}

// accept props
// vm is properly typed
54 changes: 41 additions & 13 deletions test-dts/wrapper.d-test.ts
Original file line number Diff line number Diff line change
@@ -146,20 +146,48 @@ expectType<number | undefined>(propsWrapper.props('bar'))
// @ts-expect-error :: unknown prop
propsWrapper.props('badProp')

const requiredPropsWrapper = mount(
defineComponent({
props: {
foo: { type: String, required: true },
bar: { type: Number, required: true }
}
}),
{
props: {
foo: 'abc',
bar: 123
}
// const cc = defineComponent({
// props: {
// foo: { type: String, required: true },
// bar: { type: Number, required: true }
// }
// })
// mount(
// defineComponent({
// props: {
// foo: { type: String, required: true },
// bar: { type: Number, required: true }
// }
// }),
// {
// props: {
// foo: 'aaa',
// bar: 123
// }
// }
// )

// mount({
// props: {
// a: String
// },
// setup(props) {
// props
// }
// })

const c = defineComponent({
props: {
foo: { type: String, required: true },
bar: { type: Number, required: true }
}
)
})
const requiredPropsWrapper = mount(c, {
props: {
foo: 'abc',
bar: 123
}
})

requiredPropsWrapper.setProps({
foo: 'abc'