Within your provided code snippet, the function's parameter is an array of objects. You have utilized destructuring to assign the first two array elements as car
and allStations
, each representing an object within the array:
type ICarDetails = Record<"make" | "model", string>;
const fn = (
[car, allStations]: {
car: ICarDetails;
allStations: { id: string; location: string; }[];
}[],
) => {
// This is the first element of the array:
car
//^? (parameter) car: { car: ICarDetails; allStations: { id: string; location: string; }[]; }
// This is the second element of the array:
allStations
//^? (parameter) allStations: { car: ICarDetails; allStations: { id: string; location: string; }[]; }
};
It seems that you may want to destructure only the properties of the first element:
const fn = (
[{car, allStations}]: {
car: ICarDetails;
allStations: { id: string; location: string; }[];
}[],
) => {
car
//^? (parameter) car: ICarDetails
allStations
//^? (parameter) allStations: { id: string; location: string; }[]
};
...or perhaps you intended to use an object parameter instead of an array (especially if this is a React component function):
const fn = (
{car, allStations}: {
car: ICarDetails;
allStations: { id: string; location: string; }[];
},
) => {
car
//^? (parameter) car: ICarDetails
allStations
//^? (parameter) allStations: { id: string; location: string; }[]
};
TS Playground
The complexity of the function signature may be causing confusion. By breaking down the type information into separate aliases, you can simplify the syntax within the function. This approach allows you to focus on individual parts of the overall data structure. Here's an example of simplifying the syntax with your code:
TS Playground
type Car = Record<"make" | "model", string>;
type Station = { id: string; location: string; };
type Props = {
car: Car;
allStations: Station[];
};
const fn = ({car, allStations}: Props) => {
car
//^? (parameter) car: Car
allStations
//^? (parameter) allStations: Station[]
};