Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(types): enhanced defineComponent inference #12935

Open
wants to merge 119 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
1408f22
wip [skip ci]
johnsoncodehk Feb 4, 2025
cbaf9ae
wip [skip ci]
johnsoncodehk Feb 16, 2025
be6ad27
wip [skip ci]
johnsoncodehk Feb 16, 2025
8e756fa
wip [skip ci]
johnsoncodehk Feb 17, 2025
5e9fbd1
wip [skip ci]
johnsoncodehk Feb 18, 2025
2dcae8a
wip [skip ci]
johnsoncodehk Feb 18, 2025
ebb3cbe
Merge branch 'main' into define-component-next
johnsoncodehk Feb 18, 2025
0e0630b
wip [skip ci]
johnsoncodehk Feb 18, 2025
5730a63
wip [skip ci]
johnsoncodehk Feb 18, 2025
400c45c
wip [skip ci]
johnsoncodehk Feb 18, 2025
bc66f43
wip [skip ci]
johnsoncodehk Feb 18, 2025
47db864
wip [skip ci]
johnsoncodehk Feb 18, 2025
d585eb3
wip [skip ci]
johnsoncodehk Feb 18, 2025
af21d7a
wip [skip ci]
johnsoncodehk Feb 18, 2025
ed167e9
wip [skip ci]
johnsoncodehk Feb 18, 2025
8bd560d
wip [skip ci]
johnsoncodehk Feb 18, 2025
2756e00
wip [skip ci]
johnsoncodehk Feb 18, 2025
2813eeb
wip [skip ci]
johnsoncodehk Feb 18, 2025
918ce50
wip [skip ci]
johnsoncodehk Feb 18, 2025
357fca4
Revert "wip [skip ci]"
johnsoncodehk Feb 18, 2025
d5318a9
wip [skip ci]
johnsoncodehk Feb 18, 2025
7f38e00
wip [skip ci]
johnsoncodehk Feb 18, 2025
aba56a0
wip [skip ci]
johnsoncodehk Feb 18, 2025
31b5f6c
wip [skip ci]
johnsoncodehk Feb 18, 2025
266fe53
wip [skip ci]
johnsoncodehk Feb 18, 2025
e3d44ce
wip [skip ci]
johnsoncodehk Feb 18, 2025
2c6b7ee
wip [skip ci]
johnsoncodehk Feb 18, 2025
d25cccb
wip [skip ci]
johnsoncodehk Feb 18, 2025
90043da
wip [skip ci]
johnsoncodehk Feb 18, 2025
f3a5884
wip [skip ci]
johnsoncodehk Feb 19, 2025
3369e84
wip [skip ci]
johnsoncodehk Feb 19, 2025
f25a4cb
wip [skip ci]
johnsoncodehk Feb 19, 2025
be3a3e0
wip
johnsoncodehk Feb 19, 2025
d822907
wip [skip ci]
johnsoncodehk Feb 19, 2025
2fe3bfb
wip
johnsoncodehk Feb 19, 2025
bcbf5f9
wip
johnsoncodehk Feb 19, 2025
0b002db
wip
johnsoncodehk Feb 19, 2025
53347ed
wip
johnsoncodehk Feb 19, 2025
d1ed7ca
wip
johnsoncodehk Feb 19, 2025
fe6f3bc
wip [skip ci]
johnsoncodehk Feb 20, 2025
6ba96d1
wip [skip ci]
johnsoncodehk Feb 20, 2025
244f6c1
wip
johnsoncodehk Feb 20, 2025
40b1360
wip
johnsoncodehk Feb 20, 2025
18da56d
wip
johnsoncodehk Feb 23, 2025
b78953f
wip
johnsoncodehk Feb 23, 2025
6d0acae
wip [skip ci]
johnsoncodehk Feb 24, 2025
acd0e86
wip [skip ci]
johnsoncodehk Feb 24, 2025
547b60c
wip [skip ci]
johnsoncodehk Feb 24, 2025
5d03c01
wip [skip ci]
johnsoncodehk Feb 24, 2025
2944d27
wip [skip ci]
johnsoncodehk Feb 24, 2025
ddfd406
wip [skip ci]
johnsoncodehk Feb 24, 2025
3cafd5c
wip [skip ci]
johnsoncodehk Feb 24, 2025
4d5ad54
wip
johnsoncodehk Feb 24, 2025
1681139
wip
johnsoncodehk Feb 24, 2025
0df8be2
wip
johnsoncodehk Feb 24, 2025
2aa79e0
wip
johnsoncodehk Feb 24, 2025
ef1fd2f
wip
johnsoncodehk Feb 24, 2025
704fbe6
wip
johnsoncodehk Feb 24, 2025
e89d447
wip
johnsoncodehk Feb 24, 2025
a02d8f4
wip
johnsoncodehk Feb 24, 2025
087f9de
wip
johnsoncodehk Feb 24, 2025
5cdb2de
wip
johnsoncodehk Feb 24, 2025
162b267
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 24, 2025
c164f78
wip
johnsoncodehk Feb 24, 2025
a395ec7
wip
johnsoncodehk Feb 24, 2025
47422d3
Revert "wip"
johnsoncodehk Feb 24, 2025
cde07e7
wip
johnsoncodehk Feb 24, 2025
aefdb55
wip
johnsoncodehk Feb 24, 2025
0966ab1
wip
johnsoncodehk Feb 24, 2025
697afee
wip
johnsoncodehk Feb 24, 2025
1cbc953
wip
johnsoncodehk Feb 24, 2025
74fbec8
wip
johnsoncodehk Feb 24, 2025
6cb4b3b
wip
johnsoncodehk Feb 24, 2025
bf20de1
wip
johnsoncodehk Feb 24, 2025
d14d71a
wip
johnsoncodehk Feb 24, 2025
6ad890b
wip
johnsoncodehk Feb 25, 2025
032295d
wip
johnsoncodehk Feb 25, 2025
f5706b8
wip
johnsoncodehk Feb 25, 2025
74eb495
wip
johnsoncodehk Feb 25, 2025
f50a16b
wip
johnsoncodehk Feb 25, 2025
5b0fd9e
wip
johnsoncodehk Feb 25, 2025
b002495
wip
johnsoncodehk Feb 25, 2025
700e319
wip
johnsoncodehk Feb 25, 2025
a308147
wip
johnsoncodehk Feb 25, 2025
2b2f0e2
wip
johnsoncodehk Feb 25, 2025
16b846e
wip
johnsoncodehk Feb 25, 2025
a41107d
wip
johnsoncodehk Feb 25, 2025
997f5b5
wip
johnsoncodehk Feb 25, 2025
16f62c3
wip
johnsoncodehk Feb 25, 2025
52097b3
wip
johnsoncodehk Feb 25, 2025
a031024
wip
johnsoncodehk Feb 25, 2025
a60fe81
wip
johnsoncodehk Feb 25, 2025
9d0882b
wip
johnsoncodehk Feb 25, 2025
29b44c2
wip
johnsoncodehk Feb 25, 2025
74a2a29
wip
johnsoncodehk Feb 25, 2025
2c03b8f
Revert "wip"
johnsoncodehk Feb 25, 2025
ed9d664
wip
johnsoncodehk Feb 25, 2025
79fba42
wip
johnsoncodehk Feb 25, 2025
831cfe9
wip
johnsoncodehk Feb 25, 2025
8effafc
wip
johnsoncodehk Feb 25, 2025
beb7202
wip
johnsoncodehk Feb 25, 2025
179dbe3
wip
johnsoncodehk Feb 25, 2025
20c36b9
wip
johnsoncodehk Feb 25, 2025
c27158b
wip
johnsoncodehk Feb 25, 2025
27278a0
wip
johnsoncodehk Feb 26, 2025
883fda5
wip
johnsoncodehk Feb 26, 2025
fad26f6
wip
johnsoncodehk Feb 26, 2025
8af8d11
wip
johnsoncodehk Feb 26, 2025
a29b1a0
wip
johnsoncodehk Feb 26, 2025
d4acf82
wip
johnsoncodehk Feb 26, 2025
1424ebe
wip
johnsoncodehk Feb 26, 2025
54c20e5
wip
johnsoncodehk Feb 26, 2025
c28cf90
wip
johnsoncodehk Feb 26, 2025
4fa7ac1
structured options
johnsoncodehk Feb 26, 2025
e5d0e4c
use CreateComponentPublicInstanceWithMixins
johnsoncodehk Feb 26, 2025
419b578
dont infer $options by default
johnsoncodehk Feb 26, 2025
7e2149e
add __defaults for language-tools use
johnsoncodehk Feb 26, 2025
7819a2d
Revert "add __defaults for language-tools use"
johnsoncodehk Feb 26, 2025
e9a8f8d
refactor
johnsoncodehk Feb 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 9 additions & 82 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,9 @@ describe('with object props', () => {
myProp: {
type: Number,
validator(val: unknown): boolean {
// @ts-expect-error
return val !== this.otherProp
},
default(): number {
// @ts-expect-error
return this.otherProp + 1
},
},
Expand Down Expand Up @@ -1062,7 +1060,7 @@ describe('emits', () => {
})

// emit should be valid when ComponentPublicInstance is used.
const instance = {} as ComponentPublicInstance
const instance = {} as ComponentPublicInstance<{}, {}, {}, {}, {}, string[]>
instance.$emit('test', 1)
instance.$emit('test')

Expand Down Expand Up @@ -1261,7 +1259,7 @@ describe('should allow to assign props', () => {

const Parent = defineComponent({
props: {
...Child.props,
...Child.props!,
foo: String,
},
})
Expand Down Expand Up @@ -1638,37 +1636,17 @@ describe('expose typing', () => {
})

import type {
AllowedComponentProps,
ComponentCustomProps,
ComponentInstance,
ComponentOptionsMixin,
DefineComponent,
Directive,
EmitsOptions,
ExtractPropTypes,
KeepAliveProps,
TransitionProps,
VNodeProps,
vShow,
} from 'vue'

// code generated by tsc / vue-tsc, make sure this continues to work
// so we don't accidentally change the args order of DefineComponent
declare const MyButton: DefineComponent<
{},
() => JSX.Element,
{},
{},
{},
ComponentOptionsMixin,
ComponentOptionsMixin,
EmitsOptions,
string,
VNodeProps & AllowedComponentProps & ComponentCustomProps,
Readonly<ExtractPropTypes<{}>>,
{},
{}
>
declare const MyButton: DefineComponent<{}, () => JSX.Element>
;<MyButton class="x" />

describe('__typeProps backdoor for union type for conditional props', () => {
Expand Down Expand Up @@ -1852,7 +1830,7 @@ interface ErrorMessageSlotProps {
* relying on legacy CreateComponentPublicInstance signature
*/
declare const ErrorMessage: {
new (...args: any[]): vue.CreateComponentPublicInstance<
new (...args: any[]): vue.CreateComponentPublicInstanceWithMixins<
Readonly<
vue.ExtractPropTypes<{
as: {
Expand Down Expand Up @@ -1893,8 +1871,8 @@ declare const ErrorMessage: {
unknown,
{},
{},
vue.ComponentOptionsMixin,
vue.ComponentOptionsMixin,
{},
{},
{},
vue.VNodeProps &
vue.AllowedComponentProps &
Expand All @@ -1916,58 +1894,7 @@ declare const ErrorMessage: {
},
true,
{},
{},
{
P: {}
B: {}
D: {}
C: {}
M: {}
Defaults: {}
},
Readonly<
vue.ExtractPropTypes<{
as: {
type: StringConstructor
default: any
}
name: {
type: StringConstructor
required: true
}
}>
>,
() =>
| VNode<
vue.RendererNode,
vue.RendererElement,
{
[key: string]: any
}
>
| vue.Slot<any>
| VNode<
vue.RendererNode,
vue.RendererElement,
{
[key: string]: any
}
>[]
| {
default: () => VNode<
vue.RendererNode,
vue.RendererElement,
{
[key: string]: any
}
>[]
},
{},
{},
{},
{
as: string
}
{}
>
__isFragment?: never
__isTeleport?: never
Expand Down Expand Up @@ -2013,8 +1940,8 @@ declare const ErrorMessage: {
unknown,
{},
{},
vue.ComponentOptionsMixin,
vue.ComponentOptionsMixin,
{},
{},
{},
string,
{
Expand Down
6 changes: 3 additions & 3 deletions packages-private/dts-test/functionalComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ expectType<JSX.Element>(<Bar foo={1} onUpdate={() => {}} />)
// @ts-expect-error
;<Foo baz="bar" />

const Baz: FunctionalComponent<{}, string[]> = (props, { emit }) => {
const Baz: FunctionalComponent = (props, { emit }) => {
expectType<{}>(props)
expectType<(event: string) => void>(emit)
}

expectType<Component>(Baz)

const Qux: FunctionalComponent<{}, ['foo', 'bar']> = (props, { emit }) => {
const Qux: FunctionalComponent<{}, ('foo' | 'bar')[]> = (props, { emit }) => {
emit('foo')
emit('foo', 1, 2)
emit('bar')
Expand All @@ -77,7 +77,7 @@ expectType<Component>(Qux)

const Quux: FunctionalComponent<
{},
{},
string[],
{
default: { foo: number }
optional?: { foo: number }
Expand Down
9 changes: 5 additions & 4 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import type { Mock } from 'vitest'
import {
type ComponentOptions,
type TestElement,
computed,
createApp,
Expand Down Expand Up @@ -1060,7 +1061,7 @@ describe('api: options', () => {
},
data() {
return {
plusOne: (this as any).count + 1,
plusOne: this.count + 1,
}
},
computed: {
Expand Down Expand Up @@ -1414,7 +1415,7 @@ describe('api: options', () => {
})

test('computed with setter and no getter', () => {
const Comp = {
const Comp: ComponentOptions = {
computed: {
foo: {
set() {},
Expand All @@ -1430,7 +1431,7 @@ describe('api: options', () => {

test('assigning to computed with no setter', () => {
let instance: any
const Comp = {
const Comp: ComponentOptions = {
computed: {
foo: {
get() {},
Expand Down Expand Up @@ -1482,7 +1483,7 @@ describe('api: options', () => {
})

test('methods property is not a function', () => {
const Comp = {
const Comp: ComponentOptions = {
methods: {
foo: 1,
},
Expand Down
40 changes: 37 additions & 3 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ describe('component props', () => {
render(
h(Comp, {
// absent should cast to false
// @ts-expect-error
bar: '', // empty string should cast to true
// @ts-expect-error
baz: 'baz', // same string should cast to true
// @ts-expect-error
qux: 'ok', // other values should be left in-tact (but raise warning)
}),
nodeOps.createElement('div'),
Expand Down Expand Up @@ -206,17 +209,42 @@ describe('component props', () => {
expect(proxy.bar).toBe(prevBar)
expect(defaultFn).toHaveBeenCalledTimes(1)

render(h(Comp, { bar: { b: 2 } }), root)
render(
h(Comp, {
bar: {
// @ts-expect-error
b: 2,
},
}),
root,
)
expect(proxy.foo).toBe(1)
expect(proxy.bar).toEqual({ b: 2 })
expect(defaultFn).toHaveBeenCalledTimes(1)

render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
render(
h(Comp, {
foo: 3,
bar: {
// @ts-expect-error
b: 3,
},
}),
root,
)
expect(proxy.foo).toBe(3)
expect(proxy.bar).toEqual({ b: 3 })
expect(defaultFn).toHaveBeenCalledTimes(1)

render(h(Comp, { bar: { b: 4 } }), root)
render(
h(Comp, {
bar: {
// @ts-expect-error
b: 4,
},
}),
root,
)
expect(proxy.foo).toBe(1)
expect(proxy.bar).toEqual({ b: 4 })
expect(defaultFn).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -420,13 +448,19 @@ describe('component props', () => {
}
render(
h(Comp, {
// @ts-expect-error
bool: 'true',
// @ts-expect-error
str: 100,
// @ts-expect-error
num: '100',
// @ts-expect-error
arr: {},
obj: 'false',
cls: {},
// @ts-expect-error
fn: true,
// @ts-expect-error
skipCheck: 'foo',
empty: [1, 2, 3],
}),
Expand Down
Loading