0

Being a beginner to typescript, I'm wrapping my head around the type notations. I can across lot of solutions, in github to define it as useState<any> , but in my case it isn't array of objects.

code:

export default async function xhttp(
  input: NetworkParam,
  config?: NetworkRequestConfig
): Promise<NetworkSuccessResponse | NetworkErrorResponse | any> {
  try {
    const CancelToken = axios.CancelToken;
    const source = CancelToken.source();
    if (config && config.cancelPrevious) {
      input.cancelToken = source.token;

      // remove previous from network store
      const storeState = store.getState();
      if (storeState && "network" in storeState) {
        const networkState = storeState.network;

        // cancel previous
        if (config.requestType && networkState.hasOwnProperty(config.requestType)) {
          try {
            networkState[config.requestType].cancel();
          } catch (e) {
            console.log(e);
          }
        }
      }

I get : property "network" does not exists on 'never'.

This above code is a .tsx file.

Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
  • 1
    I guess the error must be on this line? `const networkState = storeState.network;`? There's not enough there to tell, but if you're using a TypeScript aware editor like VSCode, hover over `storeState` and it will probably tell you the type is `never`, which means it's impossible to assign a value. You can work it out by hovering over various variables. – user2740650 Jun 30 '21 at 03:44
  • Yeah storeState has a type never which doesn't allow to assign any value. But I haven't set 'never' type explicitly . Or is there a way to change it? @user2740650 – Sai Krishnadas Jun 30 '21 at 03:50
  • TypeScript figures out what type a variable can be, and in this case it decided it was of type `never`. You didn't explicitly declare `never` and it's not common to do so. I was suggesting that you hover over various variables in that block of code (and same variable in different places) and let it tell you what it thinks the type is at various places. It will help you figure out where the problem is, but without a self-contained example I can only guess. – user2740650 Jun 30 '21 at 11:24
  • Please consider modifying the code in this question so as to constitute a [mcve] which, when dropped into a standalone IDE like [The TypeScript Playground (link to code)](https://tsplay.dev/WJ9zrm), clearly demonstrates the issue you are facing. This will allow those who want to help you to immediately get to work solving the problem without first needing to re-create it. And it will make it so that any answer you get is testable against a well-defined use case. – jcalz Jun 30 '21 at 18:20

0 Answers0