From d804353297f1e5adf8ea3167d171751dc79fc725 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 24 Aug 2022 07:25:23 +0200 Subject: [PATCH 1/5] improve types for bare createElement and h calls --- src/index.d.ts | 52 +++++++++++++++++++++++++++++++++++++--------- test/ts/preact.tsx | 7 ++++++- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index e89a841193..a2f608e162 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -187,36 +187,68 @@ export abstract class Component { // Preact createElement // ----------------------------------- -export function createElement( +export function createElement< + P extends JSXInternal.HTMLAttributes, + T extends HTMLElement +>( + type: keyof JSXInternal.IntrinsicElements, + props: (ClassAttributes & P) | null, + ...children: ComponentChildren[] +): VNode; +export function createElement< + P extends JSXInternal.HTMLAttributes, + T extends HTMLElement +>( + type: keyof JSXInternal.IntrinsicElements, + props: (ClassAttributes & P) | null, + ...children: ComponentChildren[] +): VNode; +export function createElement( type: string, props: - | (JSXInternal.HTMLAttributes & - JSXInternal.SVGAttributes & - Record) + | (ClassAttributes & + JSXInternal.HTMLAttributes & + JSXInternal.SVGAttributes) | null, ...children: ComponentChildren[] ): VNode; export function createElement

( type: ComponentType

, - props: (Attributes & P) | null, + props: (ClassAttributes & P) | null, ...children: ComponentChildren[] ): VNode; export namespace createElement { export import JSX = JSXInternal; } -export function h( +export function h< + P extends JSXInternal.HTMLAttributes, + T extends HTMLElement +>( + type: keyof JSXInternal.IntrinsicElements, + props: (ClassAttributes & P) | null, + ...children: ComponentChildren[] +): VNode; +export function h< + P extends JSXInternal.HTMLAttributes, + T extends HTMLElement +>( + type: keyof JSXInternal.IntrinsicElements, + props: (ClassAttributes & P) | null, + ...children: ComponentChildren[] +): VNode; +export function h( type: string, props: - | (JSXInternal.HTMLAttributes & - JSXInternal.SVGAttributes & - Record) + | (ClassAttributes & + JSXInternal.HTMLAttributes & + JSXInternal.SVGAttributes) | null, ...children: ComponentChildren[] ): VNode; export function h

( type: ComponentType

, - props: (Attributes & P) | null, + props: (ClassAttributes & P) | null, ...children: ComponentChildren[] ): VNode; export namespace h { diff --git a/test/ts/preact.tsx b/test/ts/preact.tsx index 26cd1a9cc7..53563a5c51 100644 --- a/test/ts/preact.tsx +++ b/test/ts/preact.tsx @@ -5,7 +5,8 @@ import { ComponentProps, FunctionalComponent, AnyComponent, - h + h, + createRef } from '../../'; interface DummyProps { @@ -314,3 +315,7 @@ const acceptsNumberAsLength =

; const acceptsStringAsLength =
; const ReturnNull: FunctionalComponent = () => null; + +const ref = createRef(); +createElement('div', { ref: ref }, 'hi'); +h('div', { ref: ref }, 'hi'); From 4a22a04628537bce38c79a92af5ca69d027ebce1 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 24 Aug 2022 07:28:25 +0200 Subject: [PATCH 2/5] component test --- test/ts/preact.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/ts/preact.tsx b/test/ts/preact.tsx index 53563a5c51..70af4a6f12 100644 --- a/test/ts/preact.tsx +++ b/test/ts/preact.tsx @@ -319,3 +319,8 @@ const ReturnNull: FunctionalComponent = () => null; const ref = createRef(); createElement('div', { ref: ref }, 'hi'); h('div', { ref: ref }, 'hi'); + +const functionRef = createRef(); +const RefComponentTest = () =>

hi

; +createElement(RefComponentTest, { ref: functionRef }, 'hi'); +h(RefComponentTest, { ref: functionRef }, 'hi'); From 492cbb1722d12903fc4b38f90166fce690e9fc4d Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 24 Aug 2022 07:48:14 +0200 Subject: [PATCH 3/5] fix onInput events --- src/index.d.ts | 10 ++++++++++ test/ts/preact.tsx | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/index.d.ts b/src/index.d.ts index a2f608e162..be4377a076 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -187,6 +187,11 @@ export abstract class Component { // Preact createElement // ----------------------------------- +export function createElement( + type: 'input', + props: (DOMAttributes<'input'> & ClassAttributes) | null, + ...children: ComponentChildren[] +): VNode; export function createElement< P extends JSXInternal.HTMLAttributes, T extends HTMLElement @@ -221,6 +226,11 @@ export namespace createElement { export import JSX = JSXInternal; } +export function h( + type: 'input', + props: (DOMAttributes<'input'> & ClassAttributes) | null, + ...children: ComponentChildren[] +): VNode; export function h< P extends JSXInternal.HTMLAttributes, T extends HTMLElement diff --git a/test/ts/preact.tsx b/test/ts/preact.tsx index 70af4a6f12..9f13a2ab43 100644 --- a/test/ts/preact.tsx +++ b/test/ts/preact.tsx @@ -316,11 +316,19 @@ const acceptsStringAsLength =
; const ReturnNull: FunctionalComponent = () => null; +// Refs should work on elements const ref = createRef(); createElement('div', { ref: ref }, 'hi'); h('div', { ref: ref }, 'hi'); +// Refs should work on functions const functionRef = createRef(); const RefComponentTest = () =>

hi

; createElement(RefComponentTest, { ref: functionRef }, 'hi'); h(RefComponentTest, { ref: functionRef }, 'hi'); + +// Should accept onInput +const onInput = (e: h.JSX.TargetedEvent) => {}; +; +createElement('input', { onInput: onInput }); +h('input', { onInput: onInput }); From 4bf1d3dc41d7444abd1ff7ab8f65dafb3631d0c0 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 24 Aug 2022 07:50:21 +0200 Subject: [PATCH 4/5] accept refless classattributes --- src/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index be4377a076..a6900b90c7 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -219,7 +219,7 @@ export function createElement( ): VNode; export function createElement

( type: ComponentType

, - props: (ClassAttributes & P) | null, + props: (Attributes & P) | null, ...children: ComponentChildren[] ): VNode; export namespace createElement { @@ -258,7 +258,7 @@ export function h( ): VNode; export function h

( type: ComponentType

, - props: (ClassAttributes & P) | null, + props: (Attributes & P) | null, ...children: ComponentChildren[] ): VNode; export namespace h { From 4f11884fdb3ec2e09bd880ac359f807133debfe8 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 24 Aug 2022 07:57:29 +0200 Subject: [PATCH 5/5] correct generics --- src/index.d.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index a6900b90c7..93414cdaa1 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -189,7 +189,10 @@ export abstract class Component { export function createElement( type: 'input', - props: (DOMAttributes<'input'> & ClassAttributes) | null, + props: + | (JSXInternal.DOMAttributes & + ClassAttributes) + | null, ...children: ComponentChildren[] ): VNode; export function createElement< @@ -201,7 +204,7 @@ export function createElement< ...children: ComponentChildren[] ): VNode; export function createElement< - P extends JSXInternal.HTMLAttributes, + P extends JSXInternal.SVGAttributes, T extends HTMLElement >( type: keyof JSXInternal.IntrinsicElements, @@ -228,7 +231,10 @@ export namespace createElement { export function h( type: 'input', - props: (DOMAttributes<'input'> & ClassAttributes) | null, + props: + | (JSXInternal.DOMAttributes & + ClassAttributes) + | null, ...children: ComponentChildren[] ): VNode; export function h< @@ -240,7 +246,7 @@ export function h< ...children: ComponentChildren[] ): VNode; export function h< - P extends JSXInternal.HTMLAttributes, + P extends JSXInternal.SVGAttributes, T extends HTMLElement >( type: keyof JSXInternal.IntrinsicElements,