Hi as far as I understand your question I would use a type to specify the ids.
It seems you're using Types already. So defining ids as a Tuple might solve that issue.
With this Type you can enforce that the provided Ids are always exactly two numbers; no more no less.
type Ids = [number, number];
type AppProps = {
ids: Ids
}
const App = ({ids}: AppProps) => {
const [data, setData] = useState<Record<number, Record[]>>({});
useEffect(() => {
const callAPI = async () => {
ids.forEach(id => {
const result = await axios.post("example.com", {id});
setData((prev) => ({
...prev,
[id]: result ?? [],
}));
})
}
callAPI()
})
}
If you want to allow 0-2 ids you can make the numbers optional with "?"
type Ids = [number?, number?]
const value: Ids = [] // valid
const value1: Ids = [2] // valid
const value2: Ids = [2, 3] // valid
const value3: Ids = [2, 3, 4] // invalid