Default values in props with Merge Giving me a weird interface

In the situation where I want to possibly pass in prop, say arrOfStrings:
tsx
interface ComponentProps {
arrOfStrings?: string[]
}

export default function Component(props: ComponentProps) {
const merged = mergeProps(
{
arrOfStrings: [],
},
props
);
// ... etc
}
tsx
interface ComponentProps {
arrOfStrings?: string[]
}

export default function Component(props: ComponentProps) {
const merged = mergeProps(
{
arrOfStrings: [],
},
props
);
// ... etc
}
I'm getting a resulting type for merged as :
ts
const merged: {
arrOfStrings: (string[] | never[]) & (string[] | never[] | undefined);
}
ts
const merged: {
arrOfStrings: (string[] | never[]) & (string[] | never[] | undefined);
}
How can I resolve this in Typescript?
6 Replies
Jasmin 🌈💜
hm it seems that typescript doesn't know that in this case, the merged props will 100% have a arrayOfStrings property thinkies I don't know if that could be fixed by solid.js. This would be a workaround for you:
interface ComponentProps {
arrOfStrings?: string[]
}

interface FinalProps {
arrOfStrings: string[]
}

export default function Component(props: ComponentProps) {
const merged: FinalProps = mergeProps(
{
arrOfStrings: [],
},
props,
)
// ... etc
}
interface ComponentProps {
arrOfStrings?: string[]
}

interface FinalProps {
arrOfStrings: string[]
}

export default function Component(props: ComponentProps) {
const merged: FinalProps = mergeProps(
{
arrOfStrings: [],
},
props,
)
// ... etc
}
captaindaylight.
captaindaylight.•10mo ago
The problem is this is a simplified version, so I could have final props but I'm trying to avoid duplicating a large interface :/ Thank you for this answer though
Jasmin 🌈💜
oh it's because of the empty array. This also solves the problem:
interface ComponentProps {
arrOfStrings?: string[]
}

export default function Component(props: ComponentProps) {
const merged = mergeProps(
{
arrOfStrings: [] as string[],
},
props,
)
// ... etc
}
interface ComponentProps {
arrOfStrings?: string[]
}

export default function Component(props: ComponentProps) {
const merged = mergeProps(
{
arrOfStrings: [] as string[],
},
props,
)
// ... etc
}
very confusing why typescript thinks the property can be undefined if you provide a default empty array
captaindaylight.
captaindaylight.•10mo ago
ah yes, that did it! thank you
Jasmin 🌈💜
glad that I could help! I asked in the #typescript channel about it (https://discord.com/channels/722131463138705510/783844647528955931/1143634436240461824) This looks like it could be a mistake on solid's side. I will open an issue if this would be the case.
captaindaylight.
captaindaylight.•10mo ago
awesome I'll track it there!
Want results from more Discord servers?
Add your server
More Posts
Solidjs site metadata (title, description, meta image etc.) in JavascriptI'm having difficulty with setting up metadata. My site is purely client side. When I use `@solidjs/Panda CSS and Solid-Start = FOUCWhen using Panda CSS with Solid-Start, I notice there is some major FOUC happening regardless of howIs there something like a replacePathParameters method?Hello, I'm trying to do something like this with the SolidJS router: ```ts replacePathParameters("SolidStart with the static adapter and hynrid routing?Hello! Is the hybrid routing (islandsRouter) supposed to work with the static adapter in SolidStart?What should I use if I want features of both Resources and Stores?I'm using `createResource` in a `RouteDataFunc`, which fetches a JSON tree asynchrconously. I love unoUiSlider rerenders when moving sliderHello, I have an issue with noUiSlider(https://refreshless.com/nouislider/) i want to make a slider Array of components gets re-render every time that a new component is addedHi, I'm struggling to allow users to add new inputs "on-the-fly" without forcing all elements to re-<Show when={}> typescript error question.I'm encountering a type error while working with the following code: const [loggedIn, setLoggedIn] Kobalte popover not opening in testingI'm using a Kobalte popover component. It works just fine in the browser, but for some reason it isnSetting cookies (or headers in general) from `createServerData$`Is it possible to set cookies (or any other headers) from the fetcher passed to `createServerData$`?