question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed type checking on data object

See original GitHub issue

Describe the bug Hi everyone, love this lib ❤️ , but yesterday I got some inconsistencies.

So I generated an api.tsx with the CLI using my openapi.json file, everything worked fine. But when I was using the get request hook it showed that my data response type was TrendingResponse | null and vscode intellisense stoped working on data object because of the null union.

So I looked over Get.d.ts on node_modules/restful-react/lib/Get.d.ts and it was like that:

/**
 * State for the <Get /> component. These
 * are implementation details and should be
 * hidden from any consumers.
 */
export interface GetState<TData, TError> {
    data: TData | null;
    response: Response | null;
    error: GetDataError<TError> | null;
    loading: boolean;
}

As I removed the | null operator intellisense worked fine. So here is my question, am I missusing the lib or is there something wrong on my tsconfig or is a real bug?

To Reproduce I don’t know if I’m missusing the lib so I’ll just put some info about my types and hook generated.

// Main get hook
export const useTrendingByMediaTypeAndTimeWindowGET = ({
  media_type,
  time_window,
  ...props
}: UseTrendingByMediaTypeAndTimeWindowGETProps) =>
  useGet<TrendingResponse, void, void>(`/trending/${media_type}/${time_window}`, props);

export type UseTrendingByMediaTypeAndTimeWindowGETProps = Omit<
  UseGetProps<TrendingResponse, void>,
  'path'
> & {media_type: string; time_window: TimeWindowEnum};

export type TimeWindowEnum = 'day' | 'week';

export type TrendingResponse = {
  page?: number;
  total_pages?: number;
  total_results?: number;
  results?: MovieListResponseObject;
  status_message?: string;
  status_code?: number;
};

Expected behavior Show correctly autocompletion on vscode. When I remove the | null from Get.d.ts:

image

Screenshots image

image

Workspace info:

System:
    OS: macOS High Sierra 10.13.6
    CPU: (4) x64 Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
    Memory: 19.69 MB / 8.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.17.0 - /usr/local/opt/node@10/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.11.3 - /usr/local/opt/node@10/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
    Android SDK:
      API Levels: 23, 24, 26, 27, 28
      Build Tools: 23.0.1, 27.0.3, 28.0.3
      System Images: android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-24 | Intel x86 Atom_64, android-24 | Google APIs Intel x86 Atom_64, android-26 | Intel x86 Atom_64, android-26 | Google APIs Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom
  IDEs:
    Android Studio: 3.3 AI-182.5107.16.33.5264788
    Xcode: 10.1/10B61 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.4 => 0.61.4
  npmGlobalPackages:
    react-native-cli: 2.0.1
  • restful-react: 9.4.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vitorverasmcommented, Nov 29, 2019

Yea I understood that nullable type, I have to deal with it and makes total sense. I used the null check operator and it worked:

return <Text>{data!.results}</Text>

But I’ll check and show something if it comes null

About the API, I had the json file of the swagger format, so I had to translate to openapi 3.0, I think thats the problem with the types, because after I generated the data was returning void | null so I added this TrendingResponse type. But thats another issue and related to my setup because I don’t have the openapi directly.

Thanks for the explanation I’ll close this.

1reaction
fabien0102commented, Nov 29, 2019

To have intellisense on data, you need to discriminate a bit:

const MyComp = () => {
  const {data} = useMyApi();

  if (!data) {
    return "no data"
  }

  return <h1>{data.title}</h1>
}

or with a fallback value

const MyComp = () => {
  const {data} = useMyApi();

  return <h1>{data ? data.title : "no title"}</h1>
}

Bref, you need to deal with the null case somehow to hint typescript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Script Runner Error 'Failed type checking' - Stack Overflow
Just curious if anyone knows why this bit of code is causing the error. If I comment the line out everything works fine....
Read more >
Prisma type-checking fails for destructured and appended ...
Bug description. I'm using Prisma as a type-safe ORM within my typescript node environment. When I pass a restructured object to an update...
Read more >
Static type checking error - No such property: value for class
I have got this little piece of code and can't get rid of the above error. The script runs all right. But the...
Read more >
Type checking errors - Free Pascal
This error occurs when one tries to assign the result of a procedure or destructor call. A procedure or destructor returns no value...
Read more >
Type Error, Type Checking and Type Conversion - YouTube
We're releasing a free preview (first 3 hours) of our 60+ hour 100 Days of Python Bootcamp on YouTube.In this free series, you'll...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found