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

fix: tsx component type declaration #673

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions src/core/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?:
directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }),
}

const head = ctx.options.version === 2.7
? `export {}
const head = `export {}

declare module 'vue' {`
: `export {}
import { GlobalComponents } from 'vue'

declare module 'vue' {`

Expand All @@ -142,6 +140,12 @@ ${head}`
}`
}
code += '\n}\n'
if (Object.keys(declarations.component).length > 0) {
code += `
declare global {
${declarations.component.map(d => d.replace(/(.+):/, 'const $1:')).join('\n ')}
}`
}
return code
}

Expand Down
48 changes: 43 additions & 5 deletions test/__snapshots__/dts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ exports[`dts > components only 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TestComp: typeof import('test/component/TestComp')['default']
}
}
"

declare global {
const RouterLink: typeof import('vue-router')['RouterLink']
const RouterView: typeof import('vue-router')['RouterView']
const TestComp: typeof import('test/component/TestComp')['default']
}"
`;

exports[`dts > directive only 1`] = `
Expand All @@ -26,6 +33,8 @@ exports[`dts > directive only 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface ComponentCustomProperties {
vLoading: typeof import('test/directive/Loading')['default']
Expand All @@ -42,6 +51,8 @@ exports[`dts > getDeclaration 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
Expand All @@ -52,7 +63,12 @@ declare module 'vue' {
vLoading: typeof import('test/directive/Loading')['default']
}
}
"

declare global {
const RouterLink: typeof import('vue-router')['RouterLink']
const RouterView: typeof import('vue-router')['RouterView']
const TestComp: typeof import('test/component/TestComp')['default']
}"
`;

exports[`dts > parseDeclaration - has icon component like <IMdi:diceD12> 1`] = `
Expand Down Expand Up @@ -100,14 +116,21 @@ exports[`dts > vue 2.7 components only 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TestComp: typeof import('test/component/TestComp')['default']
}
}
"

declare global {
const RouterLink: typeof import('vue-router')['RouterLink']
const RouterView: typeof import('vue-router')['RouterView']
const TestComp: typeof import('test/component/TestComp')['default']
}"
`;

exports[`dts > writeDeclaration - keep unused 1`] = `
Expand All @@ -118,6 +141,8 @@ exports[`dts > writeDeclaration - keep unused 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
Expand All @@ -131,7 +156,13 @@ declare module 'vue' {
vSome: typeof import('test/directive/Some')['default']
}
}
"

declare global {
const RouterLink: typeof import('vue-router')['RouterLink']
const RouterView: typeof import('vue-router')['RouterView']
const SomeComp: typeof import('test/component/SomeComp')['default']
const TestComp: typeof import('test/component/TestComp')['default']
}"
`;

exports[`dts > writeDeclaration 1`] = `
Expand All @@ -142,6 +173,8 @@ exports[`dts > writeDeclaration 1`] = `
// Read more: https://github.com/vuejs/core/pull/3399
export {}

import { GlobalComponents } from 'vue'

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
Expand All @@ -152,5 +185,10 @@ declare module 'vue' {
vLoading: typeof import('test/directive/Loading')['default']
}
}
"

declare global {
const RouterLink: typeof import('vue-router')['RouterLink']
const RouterView: typeof import('vue-router')['RouterView']
const TestComp: typeof import('test/component/TestComp')['default']
}"
`;