10000 types(defineComponent): Support for GlobalComponents, typed Directives and respect `expose` on defineComponent by pikax · Pull Request #3399 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

types(defineComponent): Support for GlobalComponents, typed Directives and respect expose on defineComponent #3399

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

Merged
merged 43 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e2cac07
types(defineComponent): support for expose component types
pikax Mar 9, 2021
3ccdc0b
chore: add directive typing test
pikax Mar 9, 2021
e140b07
chore: exposed type suppor
pikax Mar 9, 2021
5f49e9c
chore: add global directive type
pikax Mar 9, 2021
fbb62bc
chore: fix tests
pikax Mar 9, 2021
b10bc77
chore: clean dup code
pikax Mar 10, 2021
d0b13fa
chore: add GlobalComponents and GlobalDirectives
pikax Mar 11, 2021
057bad9
chore: add Suspense, KeepAlive, Teleport to GlobalComponents
pikax Mar 13, 2021
9970b45
chore: add Transition and TransitionGroup to globalComponents
pikax Mar 13, 2021
2498929
chore: add BaseTransition ass globalComponent
pikax Mar 13, 2021
b792c6c
chore: add VShow as a globalDirective
pikax Mar 13, 2021
980dbf3
chore: add BaseTransition
pikax Mar 13, 2021
200838d
chore: correct interface with file name
pikax Mar 14, 2021
dffd7c9
Merge branch 'master' into types/refactor_defineComponent
pikax Mar 30, 2021
08c1217
chore: wrap component to get the type
pikax Mar 30, 2021
ca63ffa
chore: typed directives + VModel directive
pikax Apr 15, 2021
16352ca
chore: move arg after modifiers and add tests
pikax Apr 18, 2021
7754d7e
chore: improve tests
pikax Apr 18, 2021
6558afd
chore: add vOn directive as global
pikax Apr 23, 2021
99741b8
chore: vmodel WIP
pikax Apr 25, 2021
675a642
Merge branch 'master-upstream' into types/refactor_defineComponent
pikax Aug 15, 2021
5573d90
chore: remove bad import and fix compat render type
pikax Aug 15, 2021
a3408d7
chore: remove type
pikax Aug 15, 2021
3ecd0cb
Merge branch 'master-upstream' into types/refactor_defineComponent
pikax Jan 1, 2022
3757760
Merge branch 'main-upstream' into types/refactor_defineComponent
pikax Jan 27, 2022
37593c9
minor: fix ssrCompiler test
pikax Feb 5, 2022
b268011
minor: remove breaking code -.-'
pikax Feb 5, 2022
5b82ea4
Merge branch 'main-upstream' into types/refactor_defineComponent
pikax Feb 5, 2022
1269ab1
minor: fix tests and remove unnecessary defineComponent
pikax Feb 5, 2022
50b17d4
chore: remove added defineComponent
pikax Feb 5, 2022
90a5872
Merge branch 'main-upstream' into types/refactor_defineComponent
pikax May 11, 2022
fa99b42
fix bad merge
pikax May 11, 2022
a59d3c5
Merge branch 'main-upstream' into types/refactor_defineComponent
pikax May 12, 2022
30dd076
Merge branch 'main' into pr/3399
pikax Oct 21, 2023
6f0042e
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 21, 2023
f9754a6
format and remove comment
pikax Oct 21, 2023
9b98398
chore: remove RawOptions from defineComponent
pikax Oct 21, 2023
794f6c1
Merge branch 'main' into pr/3399
pikax Nov 17, 2023
34a5391
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 17, 2023
6eda2d5
chore: fix vuetify usage
pikax Nov 22, 2023
3b5b8f2
chore: Merge branch 'minor' into pr/3399
pikax Jan 21, 2024
93b94c2
Merge branch 'minor' into types/refactor_defineComponent
pikax Apr 17, 2024
af1c810
chore: Merge branch 'minor' into types/refactor_defineComponent
yyx990803 Apr 25, 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
Prev Previous commit
Next Next commit
chore: Merge branch 'minor' into pr/3399
# Conflicts:
#	packages/dts-test/appUse.test-d.ts
#	packages/dts-test/defineComponent.test-d.tsx
#	packages/runtime-core/src/apiCreateApp.ts
#	packages/runtime-core/src/apiDefineComponent.ts
#	packages/runtime-core/src/componentOptions.ts
#	packages/runtime-core/src/componentPublicInstance.ts
#	packages/runtime-core/src/directives.ts
#	packages/runtime-core/src/index.ts
#	packages/runtime-dom/src/directives/vModel.ts
#	packages/runtime-dom/src/directives/vOn.ts
#	packages/runtime-dom/src/index.ts
  • Loading branch information
pikax committed Jan 21, 2024
commit 3b5b8f2bc3e80e6a5a74c0f6a72419c34578d72f
6 changes: 3 additions & 3 deletions packages/dts-test/appUse.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApp, App, Plugin, defineComponent } from 'vue'
import { type App, type Plugin, createApp, defineComponent } from 'vue'

const app = createApp({})

Expand Down Expand Up @@ -102,6 +102,6 @@ app.component(
defineComponent({
...aliases[key],
name: key,
aliasName: aliases[key].name
})
aliasName: aliases[key].name,
}),
)
2 changes: 1 addition & 1 deletion packages/dts-test/componentTypeExtensions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefineComponent, Directive, defineComponent } from 'vue'
import { type DefineComponent, type Directive, defineComponent } from 'vue'
import { expectType } from './utils'

declare module 'vue' {
Expand Down
58 changes: 31 additions & 27 deletions packages/dts-test/defineComponent.test-d.tsx
57A7
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import {
createApp,
defineComponent,
h,
SlotsType,
Slots,
VNode,
KeepAliveProps,
TransitionProps,
Directive,
vShow
reactive,
ref,
withKeys,
withModifiers,
} from 'vue'
import { type IsUnion, describe, expectType } from './utils'

Expand Down Expand Up @@ -1502,58 +1499,65 @@ describe('should work when props type is incompatible with setup returned type '
expectType<SizeType>(CompA.$props.size)
})

describe('withKeys and withModifiers as pro', () => {
const onKeydown = withKeys(e => {}, [''])
// @ts-expect-error invalid modifiers
const onClick = withModifiers(e => {}, [''])
;<input onKeydown={onKeydown} onClick={onClick} />
})

// #3367 expose components types
describe('expose component types', () => {
const child = defineComponent({
props: {
a: String
}
a: String,
},
})

const parent = defineComponent({
components: {
child,
child2: {
template: `<div></div>`
}
}
template: `<div></div>`,
},
},
})

expectType<typeof child>(parent.components!.child)
expectType<Component>(parent.components!.child2)

// global components
expectType<Readonly<KeepAliveProps>>(
new parent.components!.KeepAlive().$props
new parent.components!.KeepAlive().$props,
)
expectType<Readonly<KeepAliveProps>>(new child.components!.KeepAlive().$props)

// runtime-dom components
expectType<Readonly<TransitionProps>>(
new parent.components!.Transition().$props
new parent.components!.Transition().$props,
)
expectType<Readonly<TransitionProps>>(
new child.components!.Transition().$props
new child.components!.Transition().$props,
)
})

describe('directive typing', () => {
const customDirective: Directive = {
created(_) {}
created(_) {},
}

const comp = defineComponent({
props: {
a: String
a: String,
},
directives: {
customDirective,
localDirective: {
created(_, { arg }) {
expectType<string | undefined>(arg)
}
}
}
},
},
},
})

expectType<typeof customDirective>(comp.directives!.customDirective)
Expand All @@ -1567,11 +1571,11 @@ describe('expose typing', () => {
const Comp = defineComponent({
expose: ['a', 'b'],
props: {
some: String
some: String,
},
data() {
return { a: 1, b: '2', c: 1 }
}
},
})

expectType<Array<'a' | 'b'>>(Comp.expose!)
Expand All @@ -1587,18 +1591,18 @@ describe('expose typing', () => {
vm.c
})

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

// code generated by tsc / vue-tsc, make sure this continues to work
Expand Down
10 changes: 5 additions & 5 deletions packages/dts-test/directives.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { ObjectDirective, Directive, vModelText } from 'vue'
import { expectType, describe } from './utils'
import { type Directive, type ObjectDirective, vModelText } from 'vue'
import { describe, expectType } from './utils'

type ExtractBinding<T> = T extends (
el: any,
binding: infer B,
vnode: any,
prev: any
prev: any,
) => any
? B
: never

declare function testDirective<
Value,
Modifiers extends string = string,
Arg extends string = string
Arg extends string = string,
>(): ExtractBinding<Directive<any, Value, Modifiers, Arg>>

describe('vmodel', () => {
expectType<ObjectDirective<any, any, 'trim' | 'number' | 'lazy', string>>(
vModelText
vModelText,
)
// @ts-expect-error
expectType<ObjectDirective<any, any, 'not-valid', string>>(vModelText)
Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
import { NO, extend, isFunction, isObject } from '@vue/shared'
import { version } from '.'
import { installAppCompatProperties } from './compat/global'
import { NormalizedPropsOptions } from './componentProps'
import { ObjectEmitsOptions } from './componentEmits'
import { DefineComponent } from './apiDefineComponent'
import type { NormalizedPropsOptions } from './componentProps'
import type { ObjectEmitsOptions } from './componentEmits'
import type { DefineComponent } from './apiDefineComponent'

export interface App<HostElement = any> {
version: string
Expand All @@ -42,8 +42,8 @@ export interface App<HostElement = any> {
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component(name: string, component: Component | DefineComponent): this
directive(name: string): Directive | undefined
directive(name: string, directive: Directive): this
directive<T = any, V = any>(name: string): Directive<T, V> | undefined
directive<T = any, V = any>(name: string, directive: Directive<T, V>): this
mount(
rootContainer: HostElement | string,
isHydrate?: boolean,
Expand Down
28 changes: 13 additions & 15 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import type {
ComponentOptionsWithArrayProps,
ComponentOptionsWithObjectProps,
ComponentOptionsWithoutProps,
ComponentProvideOptions,
ComputedOptions,
MethodOptions,
RenderFunction,
ComponentOptionsBase,
ComponentInjectOptions,
ComponentOptions,
ComponentProvideOptions
} from './componentOptions'
import type {
AllowedComponentProps,
ComponentCustomProps,
Component,
ComponentCustomProps,
GlobalComponents,
GlobalDirectives
GlobalDirectives,
SetupContext,
} from './component'
import type {
ComponentObjectPropsOptions,
Expand All @@ -34,8 +32,8 @@ import type {
ComponentPublicInstanceConstructor,
CreateComponentPublicInstance,
} from './componentPublicInstance'
import { SlotsType } from './componentSlots'
import { Directive } from './directives'
import type { SlotsType } from './componentSlots'
import type { Directive } from './directives'

export type PublicProps = VNodeProps &
AllowedComponentProps &
Expand Down Expand Up @@ -65,7 +63,7 @@ export type DefineComponent<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
Expand Down Expand Up @@ -166,7 +164,7 @@ export function defineComponent<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
>(
options: ComponentOptionsWithoutProps<
Props,
Expand All @@ -185,7 +183,7 @@ export function defineComponent<
Directives,
Exposed,
Provide
>
>,
): DefineComponent<
Props,
RawBindings,
Expand Down Expand Up @@ -226,7 +224,7 @@ export function defineComponent<
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Readonly<{ [key in PropNames]?: any }>
Props = Readonly<{ [key in PropNames]?: any }>,
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand All @@ -245,7 +243,7 @@ export function defineComponent<
Directives,
Exposed,
Provide
>
>,
): DefineComponent<
Props,
RawBindings,
Expand Down Expand Up @@ -286,7 +284,7 @@ export function defineComponent<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand All @@ -305,7 +303,7 @@ export function defineComponent<
Directives,
Exposed,
Provide
>
>,
): DefineComponent<
PropsOptions,
RawBindings,
Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ import type { SchedulerJob } from './scheduler'
import type { LifecycleHooks } from './enums'

// Augment GlobalComponents
import { TeleportProps } from './components/Teleport'
import { SuspenseProps } from './components/Suspense'
import { KeepAliveProps } from './components/KeepAlive'
import { BaseTransitionProps } from './components/BaseTransition'
import { DefineComponent } from './apiDefineComponent'
import type { TeleportProps } from './components/Teleport'
import type { SuspenseProps } from './components/Suspense'
import type { KeepAliveProps } from './components/KeepAlive'
import type { BaseTransitionProps } from './components/BaseTransition'
import type { DefineComponent } from './apiDefineComponent'

export type Data = Record<string, unknown>

Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface ComponentOptionsBase<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II, Provide>,
ComponentInternalOptions,
ComponentCustomOptions {
Expand Down Expand Up @@ -235,7 +235,7 @@ export type ComponentOptionsWithoutProps<
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
PE = Props & EmitsToProps<E>
PE = Props & EmitsToProps<E>,
> = ComponentOptionsBase<
PE,
RawBindings,
Expand Down Expand Up @@ -294,7 +294,7 @@ export type ComponentOptionsWithArrayProps<
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>,
> = ComponentOptionsBase<
Props,
RawBindings,
Expand Down Expand Up @@ -412,7 +412,7 @@ export type ComponentOptions<
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
> = ComponentOptionsBase<
Props,
RawBindings,
Expand Down Expand Up @@ -530,7 +530,7 @@ interface LegacyOptions<
Extends extends ComponentOptionsMixin,
I extends ComponentInjectOptions,
II extends string,
Provide extends ComponentProvideOptions = ComponentProvideOptions
Provide extends ComponentProvideOptions = ComponentProvideOptions,
> {
compatConfig?: CompatConfig

Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0