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

Feature: Add defaultProps helper function #1794

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ export function mergeProps<T extends unknown[]>(...sources: T): MergeProps<T> {
return target as any;
}

export type DefaultProps<T, K extends keyof T> = MergeProps<[Required<Pick<T, K>>, T]>;
export function defaultProps<T, K extends keyof T>(
props: T,
defaults: Required<Pick<T, K>>
): DefaultProps<T, K> {
return mergeProps(defaults, props);
}
Comment on lines +268 to +274
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type DefaultableProps<T, K extends keyof T> = {
  [P in K]-?: undefined extends T[P] ? Exclude<T[P], undefined> : never;
}
- Required<Pick<T, K>>
+ DefaultableProps<T, K>

I haven't fully tested this but this should allow only defaults that aren't already necessary in props.


export type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
...{
[P in keyof K]: P extends `${number}`
Expand Down
58 changes: 46 additions & 12 deletions packages/solid/test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
createRoot,
createComponent,
mergeProps,
defaultProps,
splitProps,
createUniqueId,
createSignal,
Expand Down Expand Up @@ -225,21 +226,54 @@ describe("mergeProps", () => {
});
});

describe("Set Default Props", () => {
test("simple set", () => {
describe("defaultProps", () => {
test("empty defaults", () => {
let props: SimplePropTypes = {
get a() {
return "ji";
},
b: null,
c: "j"
get a() {
return "beep";
},
defaults: SimplePropTypes = { a: "yy", b: "ggg", d: "DD" };
props = mergeProps(defaults, props);
expect(props.a).toBe("ji");
b: null,
c: "boop"
};
props = defaultProps(props, {});
expect(props.a).toBe("beep");
expect(props.b).toBe(null);
expect(props.c).toBe("j");
expect(props.d).toBe("DD");
expect(props.c).toBe("boop");
expect(props.d).toBe(undefined);
});
it("overwrites only undefined values", () => {
let props: SimplePropTypes = {
get a() {
return "beep";
},
b: null,
c: "boop"
};
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");
expect(props.d).toBe("xxx");
});
it("allows null as a default", () => {
let props: SimplePropTypes = {
a: "defined",
c: null
};
props = defaultProps(props, {
a: null,
b: null,
c: null
});
expect(props.a).toBe("defined");
expect(props.b).toBe(null);
expect(props.c).toBe(null);
expect(props.d).toBe(undefined);
});
});

Expand Down
50 changes: 49 additions & 1 deletion packages/solid/test/component.type-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mergeProps, splitProps } from "../src";
import { mergeProps, defaultProps, splitProps } from "../src";

type Assert<T extends true> = never;
// from: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
Expand Down Expand Up @@ -182,6 +182,54 @@ function M4<T extends keyof M4Type = "a">(
type TestM9 = Assert<IsExact<M9, { a?: number; b?: number; c?: number }>>;
}

// d1: defaultProps preserves prop types
type D1Props = {
a?: string;
b: string;
c?: "one" | "two" | "three";
d: "one" | "two" | "three";
e?: [1, 2, 3];
f: [1, 2, 3];
g?: [string, number, "one" | "two", 3 | 4];
h: [string, number, "one" | "two", 3 | 4];
i?: null;
j: null;
};
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(
{} 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;
type TestD2 = Assert<
IsExact<
D2,
{
a: string;
b: "one" | "two" | "three";
c: [1, 2, 3];
d: [string, number, "one" | "two", 3 | 4];
e: null;
}
>
>;

// s1-s3: splitProps return type is correct regardless of usage
const s1 = splitProps({ a: 1, b: 2 }, ["a"]);
type S1 = typeof s1;
Expand Down