Skip to content

Commit

Permalink
Add typescript child type checking
Browse files Browse the repository at this point in the history
It also allows type inference (and checking) of function bodies. A basic
example is given in the tests that infers the type of `num`
  • Loading branch information
Alexendoo committed May 28, 2018
1 parent 2d0e76e commit d7d7b62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/preact.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export as namespace preact;
declare namespace preact {
type Key = string | number;
type Ref<T> = (instance: T) => void;
type ComponentChild = JSX.Element | string | number | null;
type ComponentChildren = ComponentChild[];
type ComponentChild = VNode<any> | string | number | null;
type ComponentChildren = ComponentChild[] | ComponentChild | object | string | number | null;

/**
* @deprecated
Expand Down Expand Up @@ -104,12 +104,12 @@ declare namespace preact {
function h<P>(
node: ComponentFactory<P>,
params: Attributes & P | null,
...children: (ComponentChild | ComponentChildren)[]
...children: ComponentChildren[]
): VNode<any>;
function h(
node: string,
params: JSX.HTMLAttributes & JSX.SVGAttributes & Record<string, any> | null,
...children: (ComponentChild | ComponentChildren)[]
...children: ComponentChildren[]
): VNode<any>;

function render(node: ComponentChild, parent: Element | Document, mergeWith?: Element): Element;
Expand All @@ -136,6 +136,10 @@ declare global {
props: any;
}

interface ElementChildrenAttribute {
children: any;
}

interface SVGAttributes extends HTMLAttributes {
accentHeight?: number | string;
accumulate?: "none" | "sum";
Expand Down
6 changes: 6 additions & 0 deletions test/ts/VNode-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ describe("VNode", () => {
expect(comp.children[1]).to.be.a("string");
});
});

class TypedChildren extends Component<{children: (num: number) => string}> {
render() { return null }
}

const typedChild = <TypedChildren>{num => num.toFixed(2)}</TypedChildren>

0 comments on commit d7d7b62

Please sign in to comment.