Skip to content

Commit ca4ec33

Browse files
committed
fix(type): allow provide prop types to createComponent()
Resolves: #82
1 parent f585005 commit ca4ec33

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/component/component.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type SetupFunction<Props, RawBindings> = (
5656
ctx: SetupContext
5757
) => RawBindings | RenderFunction<Props>;
5858

59-
export interface ComponentOptions<
59+
interface ComponentOptions<
6060
PropsOptions = ComponentPropsOptions,
6161
RawBindings = Data,
6262
Props = ExtractPropTypes<PropsOptions>
@@ -65,10 +65,23 @@ export interface ComponentOptions<
6565
setup?: SetupFunction<Props, RawBindings>;
6666
}
6767

68+
// Conditional returns can enforce identical types.
69+
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
70+
// prettier-ignore
71+
type Equal<Left, Right> =
72+
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false;
73+
74+
type HasDefined<T> = Equal<T, unknown> extends true ? false : true;
75+
6876
// object format with object props declaration
6977
// see `ExtractPropTypes` in ./componentProps.ts
70-
export function createComponent<PropsOptions, RawBindings>(
71-
options: ComponentOptions<PropsOptions, RawBindings> &
78+
export function createComponent<Props, RawBindings = Data, PropsOptions = ComponentPropsOptions>(
79+
// prettier-ignore
80+
options: (
81+
// prefer the provided Props, otherwise infer it from PropsOptions
82+
HasDefined<Props> extends true
83+
? ComponentOptions<PropsOptions, RawBindings, Props>
84+
: ComponentOptions<PropsOptions, RawBindings>) &
7285
Omit<Vue2ComponentOptions<Vue>, keyof ComponentOptions<never, never>>
7386
): VueProxy<PropsOptions, RawBindings>;
7487
// implementation, close to no-op

0 commit comments

Comments
 (0)