I am facing a scenario where I have an interface containing two optional properties:
interface A {
one?: string;
two?: string;
}
I want to pass these properties inside an array to a component that specifically requires string[]
export const MyComponent: React.FunctionComponent<A> = ({one, two}) => {
return <Query props={[one, two].filter(Boolean)} />
}
Even after filtering out the undefined values using filter(Boolean)
, TypeScript still raises this error:
Type '(string | undefined)[]' is not assignable to type 'string[]'.
Is there any design flaw here? Should I resort to typecasting by doing something like:
return <Query props={[one, two].filter(Boolean) as string[]} />
?