Skip to content

Commit c146186

Browse files
committed
fix(types): fix compat with generated types that rely on CreateComponentPublicInstance
close #10842
1 parent 124c4ca commit c146186

File tree

6 files changed

+273
-14
lines changed

6 files changed

+273
-14
lines changed

packages/dts-test/defineComponent.test-d.tsx

+193
Original file line numberDiff line numberDiff line change
@@ -1766,3 +1766,196 @@ defineComponent({
17661766
expectType<string | null | undefined>(props.foo)
17671767
},
17681768
})
1769+
1770+
import type * as vue from 'vue'
1771+
1772+
interface ErrorMessageSlotProps {
1773+
message: string | undefined
1774+
}
1775+
/**
1776+
* #10842
1777+
* component types generated by vue-tsc
1778+
* relying on legacy CreateComponentPublicInstance signature
1779+
*/
1780+
declare const ErrorMessage: {
1781+
new (...args: any[]): vue.CreateComponentPublicInstance<
1782+
Readonly<
1783+
vue.ExtractPropTypes<{
1784+
as: {
1785+
type: StringConstructor
1786+
default: any
1787+
}
1788+
name: {
1789+
type: StringConstructor
1790+
required: true
1791+
}
1792+
}>
1793+
>,
1794+
() =>
1795+
| VNode<
1796+
vue.RendererNode,
1797+
vue.RendererElement,
1798+
{
1799+
[key: string]: any
1800+
}
1801+
>
1802+
| vue.Slot<any>
1803+
| VNode<
1804+
vue.RendererNode,
1805+
vue.RendererElement,
1806+
{
1807+
[key: string]: any
1808+
}
1809+
>[]
1810+
| {
1811+
default: () => VNode<
1812+
vue.RendererNode,
1813+
vue.RendererElement,
1814+
{
1815+
[key: string]: any
1816+
}
1817+
>[]
1818+
},
1819+
unknown,
1820+
{},
1821+
{},
1822+
vue.ComponentOptionsMixin,
1823+
vue.ComponentOptionsMixin,
1824+
{},
1825+
vue.VNodeProps &
1826+
vue.AllowedComponentProps &
1827+
vue.ComponentCustomProps &
1828+
Readonly<
1829+
vue.ExtractPropTypes<{
1830+
as: {
1831+
type: StringConstructor
1832+
default: any
1833+
}
1834+
name: {
1835+
type: StringConstructor
1836+
required: true
1837+
}
1838+
}>
1839+
>,
1840+
{
1841+
as: string
1842+
},
1843+
true,
1844+
{},
1845+
{},
1846+
{
1847+
P: {}
1848+
B: {}
1849+
D: {}
1850+
C: {}
1851+
M: {}
1852+
Defaults: {}
1853+
},
1854+
Readonly<
1855+
vue.ExtractPropTypes<{
1856+
as: {
1857+
type: StringConstructor
1858+
default: any
1859+
}
1860+
name: {
1861+
type: StringConstructor
1862+
required: true
1863+
}
1864+
}>
1865+
>,
1866+
() =>
1867+
| VNode<
1868+
vue.RendererNode,
1869+
vue.RendererElement,
1870+
{
1871+
[key: string]: any
1872+
}
1873+
>
1874+
| vue.Slot<any>
1875+
| VNode<
1876+
vue.RendererNode,
1877+
vue.RendererElement,
1878+
{
1879+
[key: string]: any
1880+
}
1881+
>[]
1882+
| {
1883+
default: () => VNode<
1884+
vue.RendererNode,
1885+
vue.RendererElement,
1886+
{
1887+
[key: string]: any
1888+
}
1889+
>[]
1890+
},
1891+
{},
1892+
{},
1893+
{},
1894+
{
1895+
as: string
1896+
}
1897+
>
1898+
__isFragment?: never
1899+
__isTeleport?: never
1900+
__isSuspense?: never
1901+
} & vue.ComponentOptionsBase<
1902+
Readonly<
1903+
vue.ExtractPropTypes<{
1904+
as: {
1905+
type: StringConstructor
1906+
default: any
1907+
}
1908+
name: {
1909+
type: StringConstructor
1910+
required: true
1911+
}
1912+
}>
1913+
>,
1914+
() =>
1915+
| VNode<
1916+
vue.RendererNode,
1917+
vue.RendererElement,
1918+
{
1919+
[key: string]: any
1920+
}
1921+
>
1922+
| vue.Slot<any>
1923+
| VNode<
1924+
vue.RendererNode,
1925+
vue.RendererElement,
1926+
{
1927+
[key: string]: any
1928+
}
1929+
>[]
1930+
| {
1931+
default: () => VNode<
1932+
vue.RendererNode,
1933+
vue.RendererElement,
1934+
{
1935+
[key: string]: any
1936+
}
1937+
>[]
1938+
},
1939+
unknown,
1940+
{},
1941+
{},
1942+
vue.ComponentOptionsMixin,
1943+
vue.ComponentOptionsMixin,
1944+
{},
1945+
string,
1946+
{
1947+
as: string
1948+
},
1949+
{},
1950+
string,
1951+
{}
1952+
> &
1953+
vue.VNodeProps &
1954+
vue.AllowedComponentProps &
1955+
vue.ComponentCustomProps &
1956+
(new () => {
1957+
$slots: {
1958+
default: (arg: ErrorMessageSlotProps) => VNode[]
1959+
}
1960+
})
1961+
;<ErrorMessage name="password" class="error" />

packages/runtime-core/src/apiDefineComponent.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { extend, isFunction } from '@vue/shared'
3131
import type { VNodeProps } from './vnode'
3232
import type {
3333
ComponentPublicInstanceConstructor,
34-
CreateComponentPublicInstance,
34+
CreateComponentPublicInstanceWithMixins,
3535
} from './componentPublicInstance'
3636
import type { SlotsType } from './componentSlots'
3737
import type { Directive } from './directives'
@@ -68,7 +68,7 @@ export type DefineComponent<
6868
Provide extends ComponentProvideOptions = ComponentProvideOptions,
6969
MakeDefaultsOptional extends boolean = true,
7070
> = ComponentPublicInstanceConstructor<
71-
CreateComponentPublicInstance<
71+
CreateComponentPublicInstanceWithMixins<
7272
Props,
7373
RawBindings,
7474
D,
@@ -116,7 +116,7 @@ export type DefineSetupFnComponent<
116116
PP = PublicProps,
117117
> = new (
118118
props: Props & PP,
119-
) => CreateComponentPublicInstance<
119+
) => CreateComponentPublicInstanceWithMixins<
120120
Props,
121121
{},
122122
{},
@@ -240,7 +240,7 @@ export function defineComponent<
240240
Provide
241241
> &
242242
ThisType<
243-
CreateComponentPublicInstance<
243+
CreateComponentPublicInstanceWithMixins<
244244
ResolvedProps,
245245
SetupBindings,
246246
Data,

packages/runtime-core/src/componentOptions.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import type {
6262
import type { Directive } from './directives'
6363
import {
6464
type ComponentPublicInstance,
65-
type CreateComponentPublicInstance,
65+
type CreateComponentPublicInstanceWithMixins,
6666
type IntersectionMixin,
6767
type UnwrapMixinsType,
6868
isReservedPrefix,
@@ -263,7 +263,7 @@ export type ComponentOptions<
263263
Provide
264264
> &
265265
ThisType<
266-
CreateComponentPublicInstance<
266+
CreateComponentPublicInstanceWithMixins<
267267
{},
268268
RawBindings,
269269
D,
@@ -372,7 +372,7 @@ interface LegacyOptions<
372372
// since that leads to some sort of circular inference and breaks ThisType
373373
// for the entire component.
374374
data?: (
375-
this: CreateComponentPublicInstance<
375+
this: CreateComponentPublicInstanceWithMixins<
376376
Props,
377377
{},
378378
{},
@@ -381,7 +381,7 @@ interface LegacyOptions<
381381
Mixin,
382382
Extends
383383
>,
384-
vm: CreateComponentPublicInstance<
384+
vm: CreateComponentPublicInstanceWithMixins<
385385
Props,
386386
{},
387387
{},
@@ -1125,7 +1125,7 @@ export type ComponentOptionsWithoutProps<
11251125
*/
11261126
__typeEmits?: TE
11271127
} & ThisType<
1128-
CreateComponentPublicInstance<
1128+
CreateComponentPublicInstanceWithMixins<
11291129
PE,
11301130
RawBindings,
11311131
D,
@@ -1187,7 +1187,7 @@ export type ComponentOptionsWithArrayProps<
11871187
> & {
11881188
props: PropNames[]
11891189
} & ThisType<
1190-
CreateComponentPublicInstance<
1190+
CreateComponentPublicInstanceWithMixins<
11911191
Props,
11921192
RawBindings,
11931193
D,
@@ -1250,7 +1250,7 @@ export type ComponentOptionsWithObjectProps<
12501250
> & {
12511251
props: PropsOptions & ThisType<void>
12521252
} & ThisType<
1253-
CreateComponentPublicInstance<
1253+
CreateComponentPublicInstanceWithMixins<
12541254
Props,
12551255
RawBindings,
12561256
D,

packages/runtime-core/src/componentPublicInstance.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,71 @@ export type ComponentPublicInstanceConstructor<
150150
new (...args: any[]): T
151151
}
152152

153+
/**
154+
* @deprecated This is no longer used internally, but exported and relied on by
155+
* existing library types generated by vue-tsc.
156+
*/
153157
export type CreateComponentPublicInstance<
158+
P = {},
159+
B = {},
160+
D = {},
161+
C extends ComputedOptions = {},
162+
M extends MethodOptions = {},
163+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
164+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
165+
E extends EmitsOptions = {},
166+
PublicProps = P,
167+
Defaults = {},
168+
MakeDefaultsOptional extends boolean = false,
169+
I extends ComponentInjectOptions = {},
170+
S extends SlotsType = {},
171+
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
172+
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
173+
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
174+
PublicD = UnwrapMixinsType<PublicMixin, 'D'> & EnsureNonVoid<D>,
175+
PublicC extends ComputedOptions = UnwrapMixinsType<PublicMixin, 'C'> &
176+
EnsureNonVoid<C>,
177+
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
178+
EnsureNonVoid<M>,
179+
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
180+
EnsureNonVoid<Defaults>,
181+
> = ComponentPublicInstance<
182+
PublicP,
183+
PublicB,
184+
PublicD,
185+
PublicC,
186+
PublicM,
187+
E,
188+
PublicProps,
189+
PublicDefaults,
190+
MakeDefaultsOptional,
191+
ComponentOptionsBase<
192+
P,
193+
B,
194+
D,
195+
C,
196+
M,
197+
Mixin,
198+
Extends,
199+
E,
200+
string,
201+
Defaults,
202+
{},
203+
string,
204+
S
205+
>,
206+
I,
207+
S
208+
>
209+
210+
/**
211+
* This is the same as `CreateComponentPublicInstance` but adds local components,
212+
* global directives, exposed, and provide inference.
213+
* It changes the arguments order so that we don't need to repeat mixin
214+
* inference everywhere internally, but it has to be a new type to avoid
215+
* breaking types that relies on previous arguments order (#10842)
216+
*/
217+
export type CreateComponentPublicInstanceWithMixins<
154218
P = {},
155219
B = {},
156220
D = {},
@@ -167,6 +231,8 @@ export type CreateComponentPublicInstance<
167231
LC extends Record<string, Component> = {},
168232
Directives extends Record<string, Directive> = {},
169233
Exposed extends string = string,
234+
Provide extends ComponentProvideOptions = ComponentProvideOptions,
235+
// mixin inference
170236
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
171237
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
172238
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
@@ -177,7 +243,6 @@ export type CreateComponentPublicInstance<
177243
EnsureNonVoid<M>,
178244
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
179245
EnsureNonVoid<Defaults>,
180-
Provide extends ComponentProvideOptions = ComponentProvideOptions,
181246
> = ComponentPublicInstance<
182247
PublicP,
183248
PublicB,

packages/runtime-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export type {
279279
ComponentPublicInstance,
280280
ComponentCustomProperties,
281281
CreateComponentPublicInstance,
282+
CreateComponentPublicInstanceWithMixins,
282283
} from './componentPublicInstance'
283284
export type {
284285
Renderer,

0 commit comments

Comments
 (0)