as const
We use
as constto type assert. And this works great if :
function useInfiniteScroll(callback: () => void) {
const [isFetching, setIsFetching] = useState(false);
return [isFetching, setIsFetching];
}Theoretically I could return
function useInfiniteScroll(callback: () => void)
function useInfiniteScroll(callback: () => void): [boolean, Dispatch<SetStateAction<boolean>>]
// or
const returnArr:[boolean, Dispatch<SetStateAction<boolean>>] = [isFetching, setIsFetching];But those are a bit too verbose. So we could assert (aka just telling ts directly) that we know that this array won't change.
The return type of this function is a
MUTABLEarray and we want to tell TS that it's actuallyIMMUTABLEand therefore you can set the array's type as NON-widening. And Arrays and Objects arereadonlyso they never are able to change their order
Resource: https://blog.logrocket.com/const-assertions-are-the-killer-new-typescript-feature-b73451f35802/
Last updated