Closed vitorverasm closed 4 years ago
Hello @vitorverasm 👋
Can you add the related openapi specs (yaml), a classic mistake is to define:
TrendingResponse:
type: object
properties:
page:
type: number
instead of:
TrendingResponse:
type: object
properties:
page:
type: number
required:
- page
Every properties are nullable by default in openAPI so you need to specify the required
field.
For you second point, the type is totally correct, data
is well TData | null
, this is because data
is null
when loading === true
or if you have error. So you need to deal with the case (I know, it's annoying, but real life is about loading and error state also 😁)
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.
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.
Just for info, you don't need to translate your specs, our generator deal with everything (json/yaml, openapi 2 or 3) 😉
Even swagger format 🤔 ? I know I'm extending this conversation but the CLI gave and error on this specs: https://api.stoplight.io/v1/versions/9WaNJfGpnnQ76opqe/export/oas.json
It gave me this error:
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 wasTrendingResponse | null
and vscode intellisense stoped working on data object because of the null union.So I looked over
Get.d.ts
onnode_modules/restful-react/lib/Get.d.ts
and it was like that: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.
Expected behavior Show correctly autocompletion on vscode. When I remove the
| null
fromGet.d.ts
:Screenshots
Workspace info: