Skip to content

Commit

Permalink
fix(component-type-helpers): cannot infer Vue 3.4.20 functional compo…
Browse files Browse the repository at this point in the history
…nent
  • Loading branch information
johnsoncodehk committed Feb 28, 2024
1 parent cd3532d commit 4d827be
Show file tree
Hide file tree
Showing 6 changed files with 825 additions and 547 deletions.
8 changes: 4 additions & 4 deletions packages/component-meta/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/ts-component/component.ts');
const meta = checker.getComponentMeta(componentPath);

expect(meta.type).toEqual(TypeMeta.Function);
expect(meta.type).toEqual(TypeMeta.Class);

const a = meta.props.find(prop =>
prop.name === 'foo'
Expand All @@ -677,8 +677,8 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
const Foo = checker.getComponentMeta(componentPath, 'Foo');
const Bar = checker.getComponentMeta(componentPath, 'Bar');

expect(Foo.type).toEqual(TypeMeta.Function);
expect(Bar.type).toEqual(TypeMeta.Function);
expect(Foo.type).toEqual(TypeMeta.Class);
expect(Bar.type).toEqual(TypeMeta.Class);
expect(exportNames).toEqual(['Foo', 'Bar']);

const a = Foo.props.find(prop =>
Expand Down Expand Up @@ -762,7 +762,7 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/ts-component/component.tsx');
const meta = checker.getComponentMeta(componentPath);

expect(meta.type).toEqual(TypeMeta.Function);
expect(meta.type).toEqual(TypeMeta.Class);

const a = meta.props.find(prop =>
prop.name === 'foo'
Expand Down
11 changes: 5 additions & 6 deletions packages/component-type-helpers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ export declare const code: string;
export default code;

export type ComponentType<T> =
T extends new () => {} ? 1 :
T extends new (...angs: any) => {} ? 1 :
T extends (...args: any) => any ? 2 :
0;

export type ComponentProps<T> =
T extends new () => { $props: infer P; } ? NonNullable<P> :
T extends new (...angs: any) => { $props: infer P; } ? NonNullable<P> :
T extends (props: infer P, ...args: any) => any ? P :
{};

export type ComponentSlots<T> =
T extends new () => { $slots: infer S; } ? NonNullable<S> :
T extends new (...angs: any) => { $slots: infer S; } ? NonNullable<S> :
T extends (props: any, ctx: { slots: infer S; attrs: any; emit: any; }, ...args: any) => any ? NonNullable<S> :
{};

export type ComponentEmit<T> =
T extends new () => { $emit: infer E; } ? NonNullable<E> :
T extends (props: any, ctx: { slots: any; attrs: any; emit: infer E; }, ...args: any) => any ? NonNullable<E> :
T extends new (...angs: any) => { $emit: infer E; } ? NonNullable<E> :
{};

export type ComponentExposed<T> =
T extends new () => infer E ? E :
T extends new (...angs: any) => infer E ? E :
T extends (props: any, ctx: any, expose: (exposed: infer E) => any, ...args: any) => any ? NonNullable<E> :
{};
10 changes: 5 additions & 5 deletions packages/component-type-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

const code = `
export type ComponentType<T> =
T extends new () => {} ? 1 :
T extends new (...angs: any) => {} ? 1 :
T extends (...args: any) => any ? 2 :
0;
export type ComponentProps<T> =
T extends new () => { $props: infer P; } ? NonNullable<P> :
T extends new (...angs: any) => { $props: infer P; } ? NonNullable<P> :
T extends (props: infer P, ...args: any) => any ? P :
{};
export type ComponentSlots<T> =
T extends new () => { $slots: infer S; } ? NonNullable<S> :
T extends new (...angs: any) => { $slots: infer S; } ? NonNullable<S> :
T extends (props: any, ctx: { slots: infer S; attrs: any; emit: any; }, ...args: any) => any ? NonNullable<S> :
{};
export type ComponentEmit<T> =
T extends new () => { $emit: infer E; } ? NonNullable<E> :
T extends new (...angs: any) => { $emit: infer E; } ? NonNullable<E> :
T extends (props: any, ctx: { slots: any; attrs: any; emit: infer E; }, ...args: any) => any ? NonNullable<E> :
{};
export type ComponentExposed<T> =
T extends new () => infer E ? E :
T extends new (...angs: any) => infer E ? E :
T extends (props: any, ctx: { expose(exposed: infer E): any; }, ...args: any) => any ? NonNullable<E> :
{};
`.trim();
Expand Down
2 changes: 1 addition & 1 deletion packages/component-type-helpers/vue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export {
} from './index';

export type ComponentSlots<T> =
T extends new () => { $scopedSlots: infer S; } ? NonNullable<S> :
T extends new (...angs: any) => { $scopedSlots: infer S; } ? NonNullable<S> :
T extends (props: any, ctx: { slots: infer S; attrs: any; emit: any; }, ...args: any) => any ? NonNullable<S> :
{};
12 changes: 8 additions & 4 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,15 @@ exports[`vue-tsc-dts > Input: ts-component/component.ts, Output: ts-component/co
exports[`vue-tsc-dts > Input: ts-component/component.tsx, Output: ts-component/component.d.ts 1`] = `undefined`;
exports[`vue-tsc-dts > Input: ts-named-export/component.ts, Output: ts-named-export/component.d.ts 1`] = `
"export declare const Foo: (props: {
"export declare const Foo: import("vue").DefineSetupFnComponent<{
foo: string;
} & {}) => any;
export declare const Bar: (props: {
}, {}, {}, {
foo: string;
} & {}, import("vue").PublicProps>;
export declare const Bar: import("vue").DefineSetupFnComponent<{
bar?: number;
}, {}, {}, {
bar?: number;
} & {}) => any;
} & {}, import("vue").PublicProps>;
"
`;
Loading

0 comments on commit 4d827be

Please sign in to comment.