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

feat(types): new Vue() improvements (#12730) #12737

Merged
merged 6 commits into from Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions types/options.d.ts
Expand Up @@ -3,6 +3,7 @@ import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
import { SetupContext } from './v3-setup-context'
import { DebuggerEvent } from './v3-generated'
import { DefineComponent } from './v3-define-component'
import { ComponentOptionsMixin } from './v3-component-options'

type Constructor = {
new (...args: any[]): any
Expand Down Expand Up @@ -93,7 +94,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Methods,
Computed,
PropNames extends string,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -102,7 +105,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Computed,
PropNames[],
Record<PropNames, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
> &
ThisType<
CombinedVueInstance<
Expand All @@ -111,7 +116,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Methods,
Computed,
Readonly<Record<PropNames, any>>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand All @@ -124,7 +131,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -133,7 +142,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Computed,
RecordPropsDefinition<Props>,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
> &
ThisType<
CombinedVueInstance<
Expand All @@ -142,7 +153,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Methods,
Computed,
Readonly<Props>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand All @@ -158,7 +171,9 @@ export interface ComponentOptions<
Computed = DefaultComputed,
PropsDef = PropsDefinition<DefaultProps>,
Props = DefaultProps,
RawBindings = {}
RawBindings = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
> {
data?: Data
props?: PropsDef
Expand Down Expand Up @@ -217,12 +232,12 @@ export interface ComponentOptions<
}

parent?: Vue
mixins?: (ComponentOptions<Vue> | typeof Vue)[]
mixins?: (Mixin | ComponentOptions<Vue> | typeof Vue)[]
name?: string
// for SFC auto name inference w/ ts-loader check
__name?: string
// TODO: support properly inferred 'extends'
extends?: ComponentOptions<Vue> | typeof Vue
extends?: Extends | ComponentOptions<Vue> | typeof Vue
delimiters?: [string, string]
comments?: boolean
inheritAttrs?: boolean
Expand Down
39 changes: 38 additions & 1 deletion types/test/vue-test.ts
@@ -1,4 +1,4 @@
import Vue, { VNode } from '../index'
import Vue, { VNode, defineComponent } from '../index'
import { ComponentOptions } from '../options'

class Test extends Vue {
Expand Down Expand Up @@ -246,3 +246,40 @@ const ComponentWithStyleInVNodeData = Vue.extend({
])
}
})

// infer mixin type with new Vue() #12730
new Vue({
mixins: [
defineComponent({
props: {
p1: String,
p2: {
type: Number,
default: 0
}
},
data() {
return {
foo: 123
}
},
computed: {
bar() {
return 123
}
}
}),
{
methods: {
hello(n: number) {}
}
}
],
created() {
this.hello(this.foo)
this.hello(this.bar)
// @ts-expect-error
this.hello(this.p1)
this.hello(this.p2)
}
})
4 changes: 2 additions & 2 deletions types/v3-component-public-instance.d.ts
Expand Up @@ -79,11 +79,11 @@ type ExtractMixin<T> = {
Mixin: MixinToOptionTypes<T>
}[T extends ComponentOptionsMixin ? 'Mixin' : never]

type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
export type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
? OptionTypesType<{}, {}, {}, {}, {}, {}>
: UnionToIntersection<ExtractMixin<T>>

type UnwrapMixinsType<
export type UnwrapMixinsType<
T,
Type extends OptionTypesKeys
> = T extends OptionTypesType ? T[Type] : never
Expand Down