Skip to content

Fix the types for isComment and isText #6

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 1 commit into from
Jan 16, 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
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ If the passed value doesn't appear to be convertible to a VNode, the returned va
### Type

```ts
function isComment(vnode: any): vnode is (null | undefined | boolean | (VNode & { type: Comment }))
function isComment(vnode: any): vnode is (null | undefined | boolean | (VNode & { type: typeof Comment }))
```

### Description
Expand Down Expand Up @@ -346,7 +346,7 @@ Returns `true` if the passed value is a static VNode. Static VNodes are a specia
### Type

```ts
function isText(vnode: any): vnode is (string | number | (VNode & { type: Text }))
function isText(vnode: any): vnode is (string | number | (VNode & { type: typeof Text }))
```

### Description
Expand Down
26 changes: 13 additions & 13 deletions src/vue-vnode-utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
cloneVNode,
Comment,
Comment as CommentVNode,
type Component,
type ComponentOptions,
createCommentVNode,
createTextVNode,
Fragment,
Fragment as FragmentVNode,
type FunctionalComponent,
isVNode,
Static,
Text,
Static as StaticVNode,
Text as TextVNode,
type VNode,
type VNodeArrayChildren,
type VNodeChild
Expand All @@ -18,7 +18,7 @@ import {
// @ts-ignore
const DEV = process.env.NODE_ENV !== 'production'

export const isComment = (vnode: unknown): vnode is (null | undefined | boolean | (VNode & { type: Comment })) => {
export const isComment = (vnode: unknown): vnode is (null | undefined | boolean | (VNode & { type: typeof CommentVNode })) => {
return getType(vnode) === 'comment'
}

Expand All @@ -30,7 +30,7 @@ export const isElement = (vnode: unknown): vnode is (VNode & { type: string }) =
return getType(vnode) === 'element'
}

export const isFragment = (vnode: unknown): vnode is ((VNode & { type: typeof Fragment }) | VNodeArrayChildren) => {
export const isFragment = (vnode: unknown): vnode is ((VNode & { type: typeof FragmentVNode }) | VNodeArrayChildren) => {
return getType(vnode) === 'fragment'
}

Expand All @@ -42,11 +42,11 @@ export const isStatefulComponent = (vnode: unknown): vnode is (VNode & { type: C
return isComponent(vnode) && typeof vnode.type === 'object'
}

export const isStatic = (vnode: unknown): vnode is (VNode & { type: typeof Static }) => {
export const isStatic = (vnode: unknown): vnode is (VNode & { type: typeof StaticVNode }) => {
return getType(vnode) === 'static'
}

export const isText = (vnode: unknown): vnode is (string | number | (VNode & { type: Text })) => {
export const isText = (vnode: unknown): vnode is (string | number | (VNode & { type: typeof TextVNode })) => {
return getType(vnode) === 'text'
}

Expand All @@ -59,7 +59,7 @@ export const getText = (vnode: VNode | string | number): string | undefined => {
return String(vnode)
}

if (isVNode(vnode) && vnode.type === Text) {
if (isVNode(vnode) && vnode.type === TextVNode) {
return String(vnode.children)
}

Expand Down Expand Up @@ -119,13 +119,13 @@ export const getType = (vnode: unknown) => {
const typeofType = typeof type

if (typeofType === 'symbol') {
if (type === Fragment) {
if (type === FragmentVNode) {
return 'fragment'
} else if (type === Text) {
} else if (type === TextVNode) {
return 'text'
} else if (type === Comment) {
} else if (type === CommentVNode) {
return 'comment'
} else if (type === Static) {
} else if (type === StaticVNode) {
return 'static'
}
} else if (typeofType === 'string') {
Expand Down