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): provide internal options for using refs type in language tools #11492

Merged
merged 2 commits into from
Aug 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,7 @@ describe('expose typing', () => {
import type {
AllowedComponentProps,
ComponentCustomProps,
ComponentInstance,
ComponentOptionsMixin,
DefineComponent,
Directive,
Expand Down Expand Up @@ -1756,6 +1757,24 @@ describe('__typeEmits backdoor, call signature syntax', () => {
c.$emit('update', 123)
})

describe('__typeRefs backdoor, object syntax', () => {
type Refs = {
foo: number
}

const Parent = defineComponent({
__typeRefs: {} as { child: ComponentInstance<typeof Child> },
})
const Child = defineComponent({
__typeRefs: {} as Refs,
})
const c = new Parent()
const refs = c.$refs

expectType<ComponentInstance<typeof Child>>(refs.child)
expectType<number>(refs.child.$refs.foo)
})

defineComponent({
props: {
foo: [String, null],
Expand Down
12 changes: 10 additions & 2 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type DefineComponent<
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
MakeDefaultsOptional extends boolean = true,
TypeRefs extends Record<string, unknown> = {},
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstanceWithMixins<
Props,
Expand All @@ -84,7 +85,8 @@ export type DefineComponent<
S,
LC & GlobalComponents,
Directives & GlobalDirectives,
Exposed
Exposed,
TypeRefs
>
> &
ComponentOptionsBase<
Expand Down Expand Up @@ -209,6 +211,7 @@ export function defineComponent<
: { [key in RuntimePropsKeys]?: any }
: TypeProps,
ResolvedProps = Readonly<InferredProps & EmitsToProps<ResolvedEmits>>,
TypeRefs extends Record<string, unknown> = {},
>(
options: {
props?: (RuntimePropsOptions & ThisType<void>) | RuntimePropsKeys[]
Expand All @@ -220,6 +223,10 @@ export function defineComponent<
* @private for language-tools use only
*/
__typeEmits?: TypeEmits
/**
* @private for language-tools use only
*/
__typeRefs?: TypeRefs
} & ComponentOptionsBase<
ResolvedProps,
SetupBindings,
Expand Down Expand Up @@ -279,7 +286,8 @@ export function defineComponent<
Provide,
// MakeDefaultsOptional - if TypeProps is provided, set to false to use
// user props types verbatim
unknown extends TypeProps ? true : false
unknown extends TypeProps ? true : false,
TypeRefs
>

// implementation, close to no-op
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export type CreateComponentPublicInstanceWithMixins<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
TypeRefs extends Data = {},
Provide extends ComponentProvideOptions = ComponentProvideOptions,
// mixin inference
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
Expand Down Expand Up @@ -275,7 +276,8 @@ export type CreateComponentPublicInstanceWithMixins<
>,
I,
S,
Exposed
Exposed,
TypeRefs
>

export type ExposedKeys<
Expand All @@ -299,14 +301,15 @@ export type ComponentPublicInstance<
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
Exposed extends string = '',
TypeRefs extends Data = {},
> = {
$: ComponentInternalInstance
$data: D
$props: MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<Prettify<P> & PublicProps, keyof Defaults>
: Prettify<P> & PublicProps
$attrs: Data
$refs: Data
$refs: Data & TypeRefs
$slots: UnwrapSlotsType<S>
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
Expand Down