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

Enable Typescript to understand JSX default props #1181

Merged
merged 4 commits into from Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -119,7 +119,7 @@
"rollup-plugin-node-resolve": "^3.0.0",
"sinon": "^4.4.2",
"sinon-chai": "^3.0.0",
"typescript": "^2.9.0-rc",
"typescript": "^3.0.1",
"uglify-js": "^2.7.5",
"webpack": "^4.3.0"
},
Expand Down
14 changes: 14 additions & 0 deletions src/preact.d.ts
Expand Up @@ -124,6 +124,15 @@ declare namespace preact {
};
}

type Defaultize<Props, Defaults> =
// Distribute over unions
Props extends any
? // Make any properties included in Default optional
& Partial<Pick<Props, Extract<keyof Props, keyof Defaults>>>
// Include the remaining properties from Props
& Pick<Props, Exclude<keyof Props, keyof Defaults>>
: never;

declare global {
namespace JSX {
interface Element extends preact.VNode<any> {
Expand All @@ -140,6 +149,11 @@ declare global {
children: any;
}

type LibraryManagedAttributes<Component, Props> =
Component extends { defaultProps: infer Defaults }
? Defaultize<Props, Defaults>
: Props;

interface SVGAttributes extends HTMLAttributes {
accentHeight?: number | string;
accumulate?: "none" | "sum";
Expand Down
65 changes: 65 additions & 0 deletions test/ts/preact.tsx
Expand Up @@ -125,3 +125,68 @@ class ComponentWithLifecycle extends Component<DummyProps, DummyState> {
console.log("componentDidUpdate", previousProps, previousState, previousContext);
}
}

// Default props: JSX.LibraryManagedAttributes

class DefaultProps extends Component<{text: string, bool: boolean}> {
static defaultProps = {
text: "hello"
};

render() {
return <div>{this.props.text}</div>;
}
}

const d1 = <DefaultProps bool={false} text="foo" />;
const d2 = <DefaultProps bool={false} />;

class DefaultPropsWithUnion extends Component<
{ default: boolean } & (
| {
type: "string";
str: string;
}
| {
type: "number";
num: number;
})
> {
static defaultProps = {
default: true
};

render() {
return <div />;
}
}

const d3 = <DefaultPropsWithUnion type="string" str={"foo"} />;
const d4 = <DefaultPropsWithUnion type="number" num={0xf00} />;
const d5 = <DefaultPropsWithUnion type="string" str={"foo"} default={false} />;
const d6 = <DefaultPropsWithUnion type="number" num={0xf00} default={false} />;

class DefaultUnion extends Component<
| {
type: "number";
num: number;
}
| {
type: "string";
str: string;
}
> {
static defaultProps = {
type: "number",
num: 1
};

render() {
return <div />;
}
}

const d7 = <DefaultUnion />;
const d8 = <DefaultUnion num={1} />;
const d9 = <DefaultUnion type="number" />;
const d10 = <DefaultUnion type="string" str="foo" />;