Skip to content

Commit

Permalink
Switch defaultProps argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
lunafoxfire committed Jul 1, 2023
1 parent 5a125ce commit 596a719
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ export function mergeProps<T extends unknown[]>(...sources: T): MergeProps<T> {

export type DefaultProps<T, K extends keyof T> = MergeProps<[Required<Pick<T, K>>, T]>;
export function defaultProps<T, K extends keyof T>(
defaults: Required<Pick<T, K>>,
props: T
props: T,
defaults: Required<Pick<T, K>>
): DefaultProps<T, K> {
return mergeProps(defaults, props);
}
Expand Down
30 changes: 12 additions & 18 deletions packages/solid/test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe("defaultProps", () => {
b: null,
c: "boop"
};
props = defaultProps({}, props);
props = defaultProps(props, {});
expect(props.a).toBe("beep");
expect(props.b).toBe(null);
expect(props.c).toBe("boop");
Expand All @@ -249,15 +249,12 @@ describe("defaultProps", () => {
b: null,
c: "boop"
};
props = defaultProps(
{
a: "xxx",
b: "xxx",
c: "xxx",
d: "xxx"
},
props
);
props = defaultProps(props, {
a: "xxx",
b: "xxx",
c: "xxx",
d: "xxx"
});
expect(props.a).toBe("beep");
expect(props.b).toBe(null);
expect(props.c).toBe("boop");
Expand All @@ -268,14 +265,11 @@ describe("defaultProps", () => {
a: "defined",
c: null
};
props = defaultProps(
{
a: null,
b: null,
c: null
},
props
);
props = defaultProps(props, {
a: null,
b: null,
c: null
});
expect(props.a).toBe("defined");
expect(props.b).toBe(null);
expect(props.c).toBe(null);
Expand Down
16 changes: 8 additions & 8 deletions packages/solid/test/component.type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,25 @@ type D1Props = {
i?: null;
j: null;
};
const d1 = defaultProps({}, {} as D1Props);
const d1 = defaultProps({} as D1Props, {});
type D1 = typeof d1;
type TestD1 = Assert<IsExact<D1, D1Props>>;

// d2: defaultProps removes undefined on merged props
const d2 = defaultProps(
{
a: "hello",
b: "two",
c: [1, 2, 3],
d: ["string", 99, "one", 4],
e: null
},
{} as {
a?: string;
b?: "one" | "two" | "three";
c?: [1, 2, 3];
d?: [string, number, "one" | "two", 3 | 4];
e?: null;
},
{
a: "hello",
b: "two",
c: [1, 2, 3],
d: ["string", 99, "one", 4],
e: null
}
);
type D2 = typeof d2;
Expand Down

0 comments on commit 596a719

Please sign in to comment.