Im learning typescript and cant find out what does this mean. The error says Type 'FriendListItem[] | null' is not an array type.ts(2461). But when typing FriendList[], that means it will be array of FriendListItem items, right ?
interface FriendListItem {
id: number;
name: string;
address: string;
}
const App = () => {
const { data, done } = useFetchAll(
"https://jsonplaceholder.typicode.com/users"
);
const [friendList, setFriendList] = useState<FriendListItem[] | null>(null);
const addToFriendList = (friend: FriendListItem): void => {
**setFriendList((prevstate) => [...prevstate, { friend }]);**
};
const removeFromFriendList = (id: string) => {};
return (
<div className="app">
<Layout>
<Switch>
<Route path="/" exact />
<Route path="/everyone" exact>
<Everyone data={data} done={done} />
</Route>
<Route path="/everyone/:userId">
<UserDetail addToFriendList={addToFriendList} />
</Route>
</Switch>
</Layout>
</div>
);
};
export default App;
Edit: Adding my testconfig file, which is generated from cra --template typescript, did not change anything
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
[...null, { friend }]. Pass an empty array as the default value touseStateuseState<FriendListItem[]>([]);again, you can't use a spread on something that you've told the compiler might be null.