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: make createBlock work with Suspense/Teleport #2963

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/runtime-core/src/components/Suspense.ts
Expand Up @@ -82,14 +82,16 @@ export const SuspenseImpl = {
create: createSuspenseBoundary
}

// Force-casted public typing for h and TSX props inference
export const Suspense = ((__FEATURE_SUSPENSE__
? SuspenseImpl
: null) as any) as {
export type SuspenseComponentType = {
__isSuspense: true
new (): { $props: VNodeProps & SuspenseProps }
}

// Force-casted public typing for h and TSX props inference
export const Suspense = ((__FEATURE_SUSPENSE__
? SuspenseImpl
: null) as any) as SuspenseComponentType

function mountSuspense(
vnode: VNode,
container: RendererElement,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/components/Teleport.ts
Expand Up @@ -369,8 +369,9 @@ function hydrateTeleport(
return vnode.anchor && nextSibling(vnode.anchor as Node)
}

// Force-casted public typing for h and TSX props inference
export const Teleport = (TeleportImpl as any) as {
export type TeleportComponentType = {
__isTeleport: true
new (): { $props: VNodeProps & TeleportProps }
}
// Force-casted public typing for h and TSX props inference
export const Teleport = (TeleportImpl as any) as TeleportComponentType
27 changes: 19 additions & 8 deletions packages/runtime-core/src/vnode.ts
Expand Up @@ -27,16 +27,18 @@ import {
SuspenseImpl,
isSuspense,
SuspenseBoundary,
normalizeSuspenseChildren
normalizeSuspenseChildren,
SuspenseComponentType
} from './components/Suspense'
import { DirectiveBinding } from './directives'
import { TransitionHooks } from './components/BaseTransition'
import { warn } from './warning'
import { TeleportImpl, isTeleport } from './components/Teleport'
import {
currentRenderingInstance,
currentScopeId
} from './componentRenderContext'
TeleportImpl,
isTeleport,
TeleportComponentType
} from './components/Teleport'
import { currentRenderingInstance, currentScopeId } from './componentRenderContext'
import { RendererNode, RendererElement } from './renderer'
import { NULL_DYNAMIC_COMPONENT } from './helpers/resolveAssets'
import { hmrDirtyComponents } from './hmr'
Expand Down Expand Up @@ -241,7 +243,11 @@ export function setBlockTracking(value: number) {
* @private
*/
export function createBlock(
type: VNodeTypes | ClassComponent,
type:
| VNodeTypes
| ClassComponent
| TeleportComponentType
| SuspenseComponentType,
props?: Record<string, any> | null,
children?: any,
patchFlag?: number,
Expand Down Expand Up @@ -328,7 +334,12 @@ export const createVNode = (__DEV__
: _createVNode) as typeof _createVNode

function _createVNode(
type: VNodeTypes | ClassComponent | typeof NULL_DYNAMIC_COMPONENT,
type:
| VNodeTypes
| ClassComponent
| TeleportComponentType
| SuspenseComponentType
| typeof NULL_DYNAMIC_COMPONENT,
props: (Data & VNodeProps) | null = null,
children: unknown = null,
patchFlag: number = 0,
Expand Down Expand Up @@ -406,7 +417,7 @@ function _createVNode(
const vnode: VNode = {
__v_isVNode: true,
[ReactiveFlags.SKIP]: true,
type,
type: type as VNodeTypes,
props,
key: props && normalizeKey(props),
ref: props && normalizeRef(props),
Expand Down
13 changes: 12 additions & 1 deletion test-dts/component.test-d.ts
Expand Up @@ -9,7 +9,11 @@ import {
expectType,
ShallowUnwrapRef,
FunctionalComponent,
ComponentPublicInstance
ComponentPublicInstance,
createBlock,
createVNode,
Teleport,
Suspense
} from './index'

declare function extractComponentOptions<Props, RawBindings>(
Expand Down Expand Up @@ -428,3 +432,10 @@ describe('class', () => {

expectType<number>(props.foo)
})

describe('createBlock & createVNode', () => {
createBlock(Teleport)
createBlock(Suspense)
createVNode(Teleport)
createVNode(Suspense)
})