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

improve types for bare createElement and h calls #3690

Merged
merged 5 commits into from Aug 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 54 additions & 6 deletions src/index.d.ts
Expand Up @@ -188,11 +188,35 @@ export abstract class Component<P, S> {
// -----------------------------------

export function createElement(
type: 'input',
props:
| (JSXInternal.DOMAttributes<HTMLInputElement> &
ClassAttributes<HTMLInputElement>)
| null,
...children: ComponentChildren[]
): VNode<any>;
export function createElement<
P extends JSXInternal.HTMLAttributes<T>,
T extends HTMLElement
>(
type: keyof JSXInternal.IntrinsicElements,
props: (ClassAttributes<T> & P) | null,
...children: ComponentChildren[]
): VNode<any>;
export function createElement<
P extends JSXInternal.SVGAttributes<T>,
T extends HTMLElement
>(
type: keyof JSXInternal.IntrinsicElements,
props: (ClassAttributes<T> & P) | null,
...children: ComponentChildren[]
): VNode<any>;
export function createElement<T extends HTMLElement>(
type: string,
props:
| (JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes &
Record<string, any>)
| (ClassAttributes<T> &
JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes)
| null,
...children: ComponentChildren[]
): VNode<any>;
Expand All @@ -206,11 +230,35 @@ export namespace createElement {
}

export function h(
type: 'input',
props:
| (JSXInternal.DOMAttributes<HTMLInputElement> &
ClassAttributes<HTMLInputElement>)
| null,
...children: ComponentChildren[]
): VNode<any>;
export function h<
P extends JSXInternal.HTMLAttributes<T>,
T extends HTMLElement
>(
type: keyof JSXInternal.IntrinsicElements,
props: (ClassAttributes<T> & P) | null,
...children: ComponentChildren[]
): VNode<any>;
export function h<
P extends JSXInternal.SVGAttributes<T>,
T extends HTMLElement
>(
type: keyof JSXInternal.IntrinsicElements,
props: (ClassAttributes<T> & P) | null,
...children: ComponentChildren[]
): VNode<any>;
export function h<T extends HTMLElement>(
type: string,
props:
| (JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes &
Record<string, any>)
| (ClassAttributes<T> &
JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes)
| null,
...children: ComponentChildren[]
): VNode<any>;
Expand Down
20 changes: 19 additions & 1 deletion test/ts/preact.tsx
Expand Up @@ -5,7 +5,8 @@ import {
ComponentProps,
FunctionalComponent,
AnyComponent,
h
h,
createRef
} from '../../';

interface DummyProps {
Expand Down Expand Up @@ -314,3 +315,20 @@ const acceptsNumberAsLength = <div style={{ marginTop: 20 }} />;
const acceptsStringAsLength = <div style={{ marginTop: '20px' }} />;

const ReturnNull: FunctionalComponent = () => null;

// Refs should work on elements
const ref = createRef<HTMLDivElement>();
createElement('div', { ref: ref }, 'hi');
h('div', { ref: ref }, 'hi');

// Refs should work on functions
const functionRef = createRef();
const RefComponentTest = () => <p>hi</p>;
createElement(RefComponentTest, { ref: functionRef }, 'hi');
h(RefComponentTest, { ref: functionRef }, 'hi');

// Should accept onInput
const onInput = (e: h.JSX.TargetedEvent<HTMLInputElement>) => {};
<input onInput={onInput} />;
createElement('input', { onInput: onInput });
h('input', { onInput: onInput });