connectrpc / connect-query-es

TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.

Home Page:https://connectrpc.com/docs/web/query/getting-started

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returned data should be possibly undefined

AshotN opened this issue · comments

The type for the returned data in a useQuery should be potentially undefined to take into account loading and error states.

  const { data } = useQuery(
    getPrices.useQuery(),
  );

@AshotN to confirm:

  1. what version of react-query are you using?
  2. what version of TypeScript are you using?
  3. can you please post the compiler options from your TypeScript config?

Today, it appears to be working as you expect. see: https://github.com/bufbuild/connect-query/blob/683f2d4e322d4c7041cbf3990146467454599532/examples/react/basic/src/example.tsx#L33 and take a look at data. The type is SayResponse | undefined.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "components/*": ["./components/*"],
      "lib/*": ["./lib/*"]
    },
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
    "@tanstack/react-query": "^4.28.0",
    "@bufbuild/connect-query": "^0.1.1",

Typescript version: 4.9.4

I'll try to reconfirm the problem as well.

thanks: so I believe the behavior you're seeing is caused by "strictNullChecks": false, in your config. Here's a reproducible example from this repo:

"strictNullChecks": true (the default when "strict": true)

"strictNullChecks": false


unfortunately, if you configure this TypeScript compiler option this way, there's nothing we can change from the perspective of Connect Query to make the type return undefined as you're wanting.

If you set "strictNullChecks": true, it should work as you expect. If not, please let me know and we can definitely reopen!

Ah, that's my mistake. I had seen strict: true and glossed over "strictNullChecks": false

Thank you for the help, sorry for the mistake

it's a really easy one to miss! no worries! keep us posted on how it goes with Connect-Query. seriously! Would love to know how you like it and what things you'd like to see in the future. :)