score:13
The Problem
When you have a union of arrays of different types, you can't call methods on them.
The Reason
You have string[] | number[]
, and so .filter
is:
((filterFunc: (x: string) => boolean) => string[])
| ((filterFunc: (x: number) => boolean) => number[])
When TS combines those signatures, it's going to union the two filterFunc
signatures together:
((x: number) => boolean)
| ((x: string) => boolean)
This is a little unintuitive, but this simplifies to (x: number & string) => boolean
. Because if you have a function that take X, or a function that takes Y, the only safe thing to pass is something that is both X
and Y
, or X & Y
.
number & string
however, is an impossible type, which "simplifies" to never
. Hence why the signature is (x: never) => boolean
.
The Fix
Ideally you'd only use string[]
or number[]
but not both. (Just looking at the code, it's a little mysterious why id
can only be number, but "selected product ids" can be strings, too)
But if you do need to support both strings and numbers, the easiest fix here is to use Array<string | number>
instead of string[] | number[]
: the single array type doesn't have the issue of trying to union together two .filter
signatures.
You can either change your state to be of that type:
const [selectedProductIds, setSelectedProductIds] = useState<Array<string | number>>([]);
This is simple, but has the downside that it will allow arrays of mixed strings and numbers, which may not be desirable. (e.g. setSelectProductIds(['0', 1, '2'])
If not, you can temporarily cast to Array<string | number>
, do the filter, then cast back to string[] | number[]
. This is not super clean, but should be safe:
setProductPrices(
(productPrices as Array<string | number>).filter((p): boolean => !selectedProductIds.includes(p.id)) as (string[] | number[])
);
score:3
For your useState instead of
const [selectedProductIds, setSelectedProductIds] = useState<string[] | number[]>([]);
Can you try
const [selectedProductIds, setSelectedProductIds] = useState< (string|number)[]>([]);
instead?
This is because there is probably some missing code where you've set selectedProductIds
as an array of string. And in your code you've strictly defined it as 1 array and not the other. Maybe the above change fixes that. Kindly confirm.
Source: stackoverflow.com
Related Query
- Argument of type 'string | null' is not assignable to parameter of type 'ValueFn<SVGPathElement, Datum[], string | number | boolean | null>'
- React Typescript - Argument of type is not assignable to parameter of type
- Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element'. Type 'null' is not assignable to type 'Element'.ts(2345)
- Argument of type 'unknown' is not assignable to parameter of type '{}'
- Argument of type 'Element' is not assignable to parameter of type 'ReactElement<any>
- react usestate hooks error: argument of type 'yyy' is not assignable to parameter of type 'setstateaction<xx>'
- getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)'
- Argument of type '"MY_EVENTS_LOAD"' is not assignable to parameter of type 'TakeableChannel<unknown>' in yeild takeLatest
- React TypeScript: Argument is not assignable to parameter of type 'never'
- Argument of type 'string' is not assignable to parameter of type '`${string}` | `${string}.${string}` | `${string}.${number}`'
- Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
- Argument of type partial is not assignable to parameter of type
- Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction<Date>'
- React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
- Argument of type 'number' is not assignable to parameter of type 'never' in array.includes()
- Argument of type '() => () => Promise<void>' is not assignable to parameter of type 'EffectCallback'
- Getting an error Argument of type 'unknown' is not assignable to parameter of type 'Error | null'
- Redux Toolkit - Argument of type 'AsyncThunkAction<>' is not assignable to parameter of type 'AnyAction'
- React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'
- Argument of type 'Element[]' is not assignable to parameter of type 'Element'
- Redux - createStore. Argument of type is not assignable to parameter of type 'DeepPartial<any>'
- Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'
- TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement'
- Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
- Argument of type 'typeof test' is not assignable to parameter of type 'Component<{ children?: ReactNode; } & DispatchProp<any>>'
- React onclick Argument of type 'EventTarget' is not assignable to parameter of type 'Node'
- TypeScript w/ React - Argument of type 'string' is not assignable to parameter of type 'keyof T'
- Argument of type 'Function' is not assignable to parameter of type 'ComponentType<never>'
More Query from same tag
- How can I access the Autocomplete's checked boxes and store title if checked in state via hook?
- Is that possible that a component will also be re-rendered without props and states changing
- React setState callback return value
- Jest test passes locally but fails in azure devops
- Render before or after child element
- Tailwind CSS - Can't center search bar inside a div
- React state unable to update
- React JS floated tag around a component, is position absolute a prudent idea?
- Derived Computed Data from Javascript Array
- Struggling in passing data from Child to Parent React Component. Cannot update a component (`App`) while rendering a different component (`Table`)
- Beginner using chart.js: having trouble display state full of data into a column chart using variables
- I want to make a earth globe which zooms in with react-three-fiber
- Why does this React accordion scroll the page up when an element is expanded?
- Creating test payment gateway with react and stripe
- Child route is blocking the parent route`s render
- How to conditionally fetch within useEffect
- Extract single object from list in React
- React passing import name as prop and adding to image src
- When managing multiple inputs in React
- Nested route with a fixed component
- I am getting errors when deploying to Vercel. Can anyone help me with the my issues below?
- ReactJS: routing configuration issue
- Cannot save email in React.js + Express
- How to change the color of svg styled icon in React?
- React & Redux: Dispatching action not working
- GenericType in React.FC<Props<T>>
- How can I get single item's id in Ant Design table column rendering
- Push data from multiple inputs to state array
- How to use useQuery hook from ReactQuery to update state?
- How to redirect a user to any page they try to access after authentication (conditional redirect)?