KraXen72
KraXen72
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
i feel like this is a pretty common pattern, and could be documented somewhere / shown in the tutorial.
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
thanks!
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
okay, i'm pretty happy with
import { mergeProps, type ParentProps } from "solid-js";

// helper type which creates props type from defaultprops + adds children
// this would be somewhere in global.d.ts or imported.
type p<T extends Record<string, unknown>> = ParentProps<Partial<T>>

const defaultProps = {
message: "fallback message",
};

export default function Card(_props: p<typeof defaultProps>) {
const props = mergeProps(defaultProps, _props);
return <div>{props.children || props.message}</div>;
}
import { mergeProps, type ParentProps } from "solid-js";

// helper type which creates props type from defaultprops + adds children
// this would be somewhere in global.d.ts or imported.
type p<T extends Record<string, unknown>> = ParentProps<Partial<T>>

const defaultProps = {
message: "fallback message",
};

export default function Card(_props: p<typeof defaultProps>) {
const props = mergeProps(defaultProps, _props);
return <div>{props.children || props.message}</div>;
}
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
thanks so far
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
okay, i'll test that and update with my experience here
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
having to type default props this awkwardly feels like several steps backward coming from svelte, but there's enough of an IKEA effect (i built it myself, so it must be good) where i could imagine using this.
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
10 replies
SSolidJS
Created by KraXen72 on 9/24/2023 in #support
better way to type props with default props
here's my playground link
10 replies