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): DefineComponent refactor #4465

Closed
Closed
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
75e447e
feat(types): string type inferrences
pikax Sep 19, 2020
55c6c2c
chore: add h defineComponent
pikax Sep 19, 2020
b99a543
chore: v-model emit update:${props}
pikax Sep 19, 2020
83e9abd
types(defineComponent): Support emit when using functionalComponent
pikax Oct 27, 2020
e0a1656
types(slots): Add typed slots
pikax Nov 30, 2020
b4dc05f
chore: fix tests
pikax Nov 30, 2020
8809f5b
Merge branch 'master-upstream' into types/string_type_inference
pikax Dec 19, 2020
3cb7f02
chore: rename `capitalize` to Capitalize<T>
pikax Dec 19, 2020
341b350
chore: rollback package and typescript dep
pikax Dec 19, 2020
8089a6a
chore: fix build
pikax Dec 19, 2020
415460a
chore: trying to fix tests
pikax Dec 19, 2020
f24b161
chore: defineComponent working as expected with `h`
pikax Dec 19, 2020
8e07103
chore: kinda working but not really
pikax Dec 19, 2020
47de099
chore: ...
pikax Dec 19, 2020
eb87718
Merge branch 'master-upstream' into types/string_type_inference
pikax Jan 26, 2021
748550c
chore: fix test
pikax Jan 26, 2021
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
53379a1
Merge branch 'upstream' into feat/typed_slots
pikax Mar 13, 2021
d729fba
chore: added support for `h`
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
a54a692
Merge branch 'master-upstream' into types/string_type_inference
pikax Jul 16, 2021
0cf01a5
minor: fix test
pikax Jul 16, 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
7583477
chore: improve test
pikax Aug 15, 2021
5300104
chore: Merge branch 'master-upstream' into feat/typed_slots
pikax Aug 20, 2021
8a73d1e
chore: Merge branch 'feat/typed_slots' into types/Merge_DefineCompone…
pikax Aug 20, 2021
6fe8330
chore: Merge branch 'types/string_type_inference' into types/Merge_De…
pikax Aug 28, 2021
b3d2b05
chore: Merge branch 'types/definecomponent_functional_emit' into type…
pikax Aug 28, 2021
18a78af
chore: Merge branch 'types/refactor_defineComponent' into types/Merge…
pikax Aug 28, 2021
372eeb2
wip: not properly working
pikax Aug 28, 2021
847d67d
wip: improve mixin emits
pikax Aug 28, 2021
1c4ddad
chore: duplicated must Component types
pikax Aug 29, 2021
e201d2f
chore: defineComponent changes
pikax Aug 29, 2021
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"semver": "^7.3.2",
"serve": "^12.0.0",
"ts-jest": "^26.2.0",
"typescript": "^4.2.2",
"typescript": "^4.4.2",
"yorkie": "^2.0.0"
}
}
4 changes: 1 addition & 3 deletions packages/compiler-core/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,7 @@ describe('compiler: transform component slots', () => {
})

test('<slot w/ nested component>', () => {
const { slots } = parseWithSlots(
`<Comp><Comp><slot/></Comp></Comp>`
)
const { slots } = parseWithSlots(`<Comp><Comp><slot/></Comp></Comp>`)
expect(slots).toMatchObject(toMatch)
})
})
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-sfc/src/cssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { PluginCreator } from 'postcss'
import hash from 'hash-sum'

export const CSS_VARS_HELPER = `useCssVars`
export const cssVarRE = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
export const cssVarRE =
/\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g

export function genCssVarsFromList(
vars: string[],
Expand Down
133 changes: 111 additions & 22 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
import {
SetupContext,
AllowedComponentProps,
ComponentCustomProps
ComponentCustomProps,
Component,
GlobalDirectives,
GlobalComponents
} from './component'
import {
ExtractPropTypes,
Expand All @@ -25,11 +28,17 @@ import {
CreateComponentPublicInstance,
ComponentPublicInstanceConstructor
} from './componentPublicInstance'
import { Slots } from './componentSlots'
import { Directive } from './directives'

export type PublicProps = VNodeProps &
AllowedComponentProps &
ComponentCustomProps

type FixS<T extends EmitsOptions> = T extends string[]
? Record<T[number], null>
: T

export type DefineComponent<
PropsOrPropOptions = {},
RawBindings = {},
Expand All @@ -38,10 +47,14 @@ export type DefineComponent<
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S = any,
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
PP = PublicProps,
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>> & EmitsToProps<E>,
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Expand All @@ -52,14 +65,19 @@ export type DefineComponent<
M,
Mixin,
Extends,
E,
FixS<E>,
S,
PP & Props,
Defaults,
true
true,
LC & GlobalComponents,
Directives & GlobalDirectives,
Exposed
> &
Props
> &
ComponentOptionsBase<
Readonly<ExtractPropTypes<PropsOrPropOptions>>
> & /**
* just typescript
*/ { __isDefineComponent?: true } & ComponentOptionsBase<
Props,
RawBindings,
D,
Expand All @@ -69,6 +87,10 @@ export type DefineComponent<
Extends,
E,
EE,
S,
LC & GlobalComponents,
Directives & GlobalDirectives,
Exposed,
Defaults
> &
PP
Expand All @@ -80,12 +102,17 @@ export type DefineComponent<

// overload 1: direct setup function
// (uses user defined props interface)
export function defineComponent<Props, RawBindings = object>(
export function defineComponent<
Props,
RawBindings = object,
E extends EmitsOptions = {},
S = {}
>(
setup: (
props: Readonly<Props>,
ctx: SetupContext
ctx: SetupContext<E, Slots<S>>
) => RawBindings | RenderFunction
): DefineComponent<Props, RawBindings>
): DefineComponent<Props, RawBindings, {}, any, any, any, any, any, string, S>

// overload 2: object format with no props
// (uses user defined props interface)
Expand All @@ -99,7 +126,11 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string
EE extends string = string,
S = any,
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string
>(
options: ComponentOptionsWithoutProps<
Props,
Expand All @@ -110,9 +141,27 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
S,
LC,
Directives,
Exposed
>
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
): DefineComponent<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
S,
LC,
Directives,
Exposed
>

// overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: any }
Expand All @@ -125,8 +174,12 @@ export function defineComponent<
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
E extends EmitsOptions = {},
EE extends string = string,
S = any,
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand All @@ -137,7 +190,11 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
S,
LC,
Directives,
Exposed
>
): DefineComponent<
Readonly<{ [key in PropNames]?: any }>,
Expand All @@ -148,7 +205,11 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
S,
LC,
Directives,
Exposed
>

// overload 4: object format with object props declaration
Expand All @@ -163,8 +224,14 @@ export function defineComponent<
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
E extends EmitsOptions = {},
EE extends string = string,
S = any,
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Props = Readonly<ExtractPropTypes<PropsOptions>>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand All @@ -175,9 +242,31 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
S,
LC,
Directives,
Exposed,
Props,
Defaults
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
): DefineComponent<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
S,
LC,
Directives,
Exposed,
Props,
Defaults
>

// implementation, close to no-op
export function defineComponent(options: unknown) {
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/compat/componentFunctional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const normalizedFunctionalComponentMap = new Map<
FunctionalComponent
>()

export const legacySlotProxyHandlers: ProxyHandler<InternalSlots> = {
export const legacySlotProxyHandlers: ProxyHandler<
InternalSlots<Record<string, null>>
> = {
get(target, key: string) {
const slot = target[key]
return slot && slot()
Expand Down
63 changes: 54 additions & 9 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Directive, validateDirectiveName } from './directives'
import {
applyOptions,
ComponentOptions,
ComponentOptionsMixin,
ComputedOptions,
MethodOptions
} from './componentOptions'
Expand Down Expand Up @@ -70,6 +71,40 @@ export type Data = Record<string, unknown>
*/
export interface ComponentCustomProps {}

/**
* For globally defined Directives
* Here is an example of adding a directive `VTooltip` as global directive:
*
* @example
* ```ts
* import VTooltip from 'v-tooltip'
*
* declare module '@vue/runtime-core' {
* interface GlobalDirectives {
* VTooltip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should suggest using camelCase or kebab-case version of directive (without the v prefix); similar to local registration in component options.

* }
* }
* ```
*/
export interface GlobalDirectives extends Record<string, Directive> {}

/**
* For globally defined Components
* Here is an example of adding a component `RouterView` as global component:
*
* @example
* ```ts
* import { RouterView } from 'vue-router'
*
* declare module '@vue/runtime-core' {
* interface GlobalComponents {
* RouterView
* }
* }
* ```
*/
export interface GlobalComponents extends Record<string, Component> {}

/**
* Default allowed non-declared props on component in TSX
*/
Expand Down Expand Up @@ -103,10 +138,13 @@ export interface ComponentInternalOptions {
__file?: string
}

export interface FunctionalComponent<P = {}, E extends EmitsOptions = {}>
extends ComponentInternalOptions {
export interface FunctionalComponent<
P = {},
E extends EmitsOptions = {},
S extends Slots = Slots
> extends ComponentInternalOptions {
// use of any here is intentional so it can be a valid JSX Element constructor
(props: P, ctx: Omit<SetupContext<E>, 'expose'>): any
(props: P, ctx: Omit<SetupContext<E, P, S>, 'expose'>): any
props?: ComponentPropsOptions<P>
emits?: E | (keyof E)[]
inheritAttrs?: boolean
Expand All @@ -129,10 +167,13 @@ export type ConcreteComponent<
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = any,
Extends extends ComponentOptionsMixin = any,
E extends EmitsOptions = any
> =
| ComponentOptions<Props, RawBindings, D, C, M>
| FunctionalComponent<Props, any>
| ComponentOptions<Props, RawBindings, D, C, M, Mixin, Extends, E>
| FunctionalComponent<Props, E>

/**
* A type used in public APIs where a component type is expected.
Expand Down Expand Up @@ -169,10 +210,14 @@ export const enum LifecycleHooks {
SERVER_PREFETCH = 'sp'
}

export interface SetupContext<E = EmitsOptions> {
export interface SetupContext<
E = EmitsOptions,
P = {},
S extends Slots = Slots
> {
attrs: Data
slots: Slots
emit: EmitFn<E>
slots: S
emit: EmitFn<E, P>
expose: (exposed?: Record<string, any>) => void
}

Expand Down
Loading