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

types(defineComponent): Stricter Component Type + helpers #9556

Open
wants to merge 83 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
b256412
WIP: component helper types
pikax Nov 6, 2023
e1ef6a8
fix one issue
pikax Nov 6, 2023
fbd083b
working kinda
pikax Nov 6, 2023
8934665
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 6, 2023
e7c0383
add Extract component Options
pikax Nov 6, 2023
5f86b19
added types
pikax Nov 6, 2023
22c304c
more tests
pikax Nov 8, 2023
c2c0958
working few things
pikax Nov 8, 2023
3fbfe6f
cleanup
pikax Nov 8, 2023
99e48a7
removed unused
pikax Nov 8, 2023
1ce7234
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 8, 2023
d88f7af
remove stuff temp
pikax Nov 8, 2023
3b653cd
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 8, 2023
9b23cdf
small improvements
pikax Nov 9, 2023
de726ac
Merge branch 'main' into pikax/component_helper_types
pikax Nov 10, 2023
8b1faf7
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 10, 2023
ae7b51b
export LooseRequired
pikax Nov 10, 2023
9e67b17
WIP DefineComponentOptions type
pikax Nov 13, 2023
f6f82fa
WIP building
pikax Nov 14, 2023
0506beb
test passing
pikax Nov 14, 2023
3e29651
fix other issues
pikax Nov 14, 2023
48a0b08
remove render function type
pikax Nov 14, 2023
6d1db29
async component changes
pikax Nov 14, 2023
f47bc76
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 14, 2023
bbad2f8
support more overloads on defineComponet and also support VNode on h
pikax Nov 14, 2023
1d40c76
add vnode support
pikax Nov 15, 2023
998607e
few more changes
pikax Nov 15, 2023
4254fac
Merge branch 'main' into pikax/component_helper_types
pikax Nov 15, 2023
633b852
more work
pikax Nov 16, 2023
96f244a
more improvements
pikax Nov 16, 2023
aab3189
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 16, 2023
8a61db8
some improvements
pikax Nov 17, 2023
3d8a5c0
more improvements and add some comments
pikax Nov 18, 2023
ca626bf
more improvments
pikax Nov 18, 2023
b2c4a38
Merge branch 'main' into pikax/component_helper_types
pikax Nov 18, 2023
9853470
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 18, 2023
0d0bf05
fix componentSlots
pikax Nov 20, 2023
9ae88d3
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 20, 2023
f62ae36
support volar syntax for slots
pikax Nov 20, 2023
413d556
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 20, 2023
836a930
introducing DeclareComponent type
pikax Nov 20, 2023
8336ba5
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 20, 2023
77ca7b1
chore: renaming & lint
pikax Nov 21, 2023
bba1cde
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 21, 2023
ab9823d
fix test type
pikax Nov 21, 2023
c98c360
chore: some changes
pikax Nov 22, 2023
86826aa
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 22, 2023
92057db
some imrpovements
pikax Nov 22, 2023
b015918
chore: some improvements
pikax Nov 22, 2023
6819d05
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 22, 2023
f3119d4
types: change DefineComponent defaults to be more lenient
pikax Nov 23, 2023
53f2af0
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 23, 2023
018223d
chore: improve ComponentData
pikax Nov 23, 2023
5e9d759
Custom props
pikax Nov 24, 2023
73f52d3
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 24, 2023
bf6286d
kinda working
pikax Nov 25, 2023
aa91257
remov efunction
pikax Nov 25, 2023
bb4cf93
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 25, 2023
d483491
chore: fix tests
pikax Nov 28, 2023
b1094a6
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 28, 2023
b4b62d0
chore: render function
pikax Nov 28, 2023
46cc5cb
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 28, 2023
cc2ed2a
chore: remove comments
pikax Nov 28, 2023
e777107
Merge branch 'minor' into pikax/component_helper_types
pikax Dec 29, 2023
eaa2ded
type fixing
pikax Dec 29, 2023
e3d217b
fix tests
pikax Dec 29, 2023
e063b78
fix tests
pikax Dec 29, 2023
e8e937b
chore: review
pikax Dec 29, 2023
fb2e70b
chore: improve defineAsyncComponent types
pikax Dec 30, 2023
5c0c261
chore: improve tests for async component
pikax Dec 30, 2023
37ca2cb
chore: some improvements + tests
pikax Dec 31, 2023
e9c7dfc
added DeclareEmits and removed shortEmits type
pikax Jan 13, 2024
b56836f
EmitsOptions to support events
pikax Jan 13, 2024
f3b9c28
improve type resolution when using ComponentEmits on DeclareComponent
pikax Jan 13, 2024
3b307e3
chore: improve ObjectToProps, to keep the original type
pikax Jan 13, 2024
b8b99a8
chore: Added DynamicComponent type helper
pikax Jan 13, 2024
7866070
chore: fix typo
pikax Jan 13, 2024
c6c8caf
chore: add a few tests for DynamicComponent
pikax Jan 13, 2024
42301b3
chore: improve types
pikax Jan 16, 2024
3ff6732
chore: clean up types, prevent object properties being overridden
pikax Jan 16, 2024
39b9ade
chore: fix usage of ComponentProps on DeclareComponent
pikax Jan 17, 2024
5890e8c
chore: fix type inferrence on props
pikax Jan 17, 2024
bdc1d73
chore: improvements on resolving props
pikax Jan 17, 2024
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
57 changes: 57 additions & 0 deletions packages/dts-test/componentTypeHelpers.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type DeclareEmits,
type ExtractComponentOptions,
type FunctionalComponent,
type ObjectToComponentProps,
type PropType,
type SetupContext,
type SlotsType,
Expand Down Expand Up @@ -1304,6 +1305,62 @@ describe('DeclareEmits', () => {
}
})

// ObjectToComponentProps
describe('ObjectToComponentProps', () => {
{
// prop options
const props = {
foo: String,
bar: {
type: String,
default: 'bar',
},
}
const s = {} as ObjectToComponentProps<typeof props>

expectType<{
foo: StringConstructor
bar: {
type: StringConstructor
default: string
}
}>(s)

// @ts-expect-error not any
expectType<{ bar: string }>(s)
}
{
// array props
const props = ['foo', 'bar'] as const
const s = {} as ObjectToComponentProps<typeof props>

expectType<readonly ['foo', 'bar']>(s)
// @ts-expect-error not any
expectType<{ bar: string }>(s)
}

{
// types
const s = {} as ObjectToComponentProps<{
foo?: string
bar: string
}>

expectType<{
foo: {
type: PropType<string>
required: false
}
bar: {
type: PropType<string>
required: true
}
}>(s)
// @ts-expect-error not any
expectType<{ bar: string }>(s)
}
})

// Component Instance

declare function retrieveComponentInstance<T>(
Expand Down
21 changes: 13 additions & 8 deletions packages/runtime-core/src/componentTypeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
ComponentPropsOptions,
ExtractDefaultPropTypes,
ExtractPropTypes,
Prop,
PropType,
} from './componentProps'
import type { Slots, SlotsType, UnwrapSlotsType } from './componentSlots'
Expand Down Expand Up @@ -186,14 +187,18 @@ type ResolveMixin<T> = [T] extends [
* }
* ```
*/
export type ObjectToComponentProps<T> = T extends Record<string, any>
? {
[K in keyof T]-?: {
type: PropType<Exclude<T[K], undefined>>
required: undefined extends T[K] ? false : true
export type ObjectToComponentProps<T> = T extends Readonly<Array<string>>
? T
: T extends Record<string, any>
? {
[K in keyof T]-?: T[K] extends Prop<any>
? T[K]
: {
type: PropType<Exclude<T[K], undefined>>
required: undefined extends T[K] ? false : true
}
}
}
: {}
: {}

/**
* Extracts Original props from options
Expand Down Expand Up @@ -251,7 +256,7 @@ export type ComponentEmitsAsProps<T> = T extends {
* Returns runtime props definition for a component
*
* @see Include emits {@linkcode ComponentEmitsAsProps}
* @see Get the render props {@linkcode ComponentExpectedProps}
* @see Get the render props {@linkcode Compo/`nentExpectedProps}
pikax marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```ts
Expand Down