Code Monkey home page Code Monkey logo

Comments (9)

ShacharHarshuv avatar ShacharHarshuv commented on June 8, 2024 1

I don't think your proposed solution is right though. I think we should rather extend UndefinedInitialDataOptions

When looking at the types more closely I believe you are correct.

According to my understanding the idea is that UndefinedInitialDataOptions will return query result where data is possibly undefined, while DefinedInitialDataOptions returns query result where the data is required, so the consumer wouldn't need to handle the optional undefined value. According to that logic the option of () => T | undefined can result in a query that is possibly undefined, so it should be in the UndefinedInitlaDataOptions (Even though it's confusing, because the initialData is technically "defined").

I'll fix the PR accordingly.

from query.

ShacharHarshuv avatar ShacharHarshuv commented on June 8, 2024 1

okay interestingly, we have test that cover this case already, and they don't fail:

It doesn't fail because you use useQuery directly. The bug (both in angular and react) reproduces when you use queryOptions

from query.

ShacharHarshuv avatar ShacharHarshuv commented on June 8, 2024

Opened a PR that fixes this: #7351

from query.

arnoud-dv avatar arnoud-dv commented on June 8, 2024

Hi @TkDodo I'm not sure about this one. Code is identical to React adapter and I don't think initialData is supposed to be able to undefined?

from query.

ShacharHarshuv avatar ShacharHarshuv commented on June 8, 2024

The problematic types are defined in angular-query-experimental.

If an undefined return type is not supported than I'm not sure how the common use case will work. This example (from the docs) currently wouldn't work:

result = injectQuery(() => ({
  queryKey: ['todo', this.todoId()],
  queryFn: () => fetch('/todos'),
  initialData: () => {
    // Use a todo from the 'todos' query as the initial data for this todo query
    return this.queryClient
      .getQueryData(['todos'])
      ?.find((d) => d.id === this.todoId())
  },
}))

Also, this works in react-query:

const x = useQuery({
  initialData: () => undefined,
})

from query.

TkDodo avatar TkDodo commented on June 8, 2024

it should work, but it also doesn't work in react query:

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRwK4FMoE8DyYbAQB2AznAL5wBmUEIcA5AAIwCGpbAxgNYD0U6VpxgBaNJiwMAUFOBEYmKkPRwAKhAAmERFLhxgGgFxwSMKHIDmAbl1wCMADbpjp80WtTyM9AA9IsOE5iUztNCABFDGw8AmI4AF44AAoDFzNLOAAfOCJUBwcASgSAPltxaPxCUiSEWz1yrABpdCxjAG0GGDCGABp9DQBdHrqUKKwAMSJjJKL40r0FuAAFWhBgEnQAOgESCAcAN3QakcXUxgBGXpOF+ydjBgARbQAZVlQiDWwrxb1yAuHTkRgARWA4Hqw2NMCsZ1FosnB3hp0FQ5OgNCUER9kaiNACKAUrEA

I don't think your proposed solution is right though. I think we should rather extend UndefinedInitialDataOptions to be:

export type UndefinedInitialDataOptions<
  TQueryFnData = unknown,
  TError = DefaultError,
  TData = TQueryFnData,
  TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
-  initialData?: undefined
-  initialData?: undefined | () => undefined
}

thoughts ?

from query.

TkDodo avatar TkDodo commented on June 8, 2024

sorry, that doesn't work. This seems to be the correct diff:

- initialData?: undefined
+ initialData?: undefined | InitialDataFunction<TQueryFnData>

as shown in this reproduction: https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBDAnmApnA3nAIgQxjgFRwHMAabFAMxwFcAbGAUSimnIEUaUpEBpFROQCSAO2AxgOOrnwAxGiIDGEiCLgBfOJVYg4AcgAC+EQGd8igNYB6AI5ceAWkXQUegFChIsBMjSYAqiYonNyIAPJgKqYaWjr6Rjim5tZQKDjKDnah7m4oAB5e8EiocP4iACZUwCIo5aLiktJ4OBFRJgA8bnBwBCE8siIyOHAAvHAKFiIQAO4ipF09zKxQoxTU9EwsbAsEQ6u99ogDQ-PdB6H8iHD5MCgVJnB9fAKrT5fzAHyrgcGHrcCqDrnfqDZrkAhLNg9E49N4CL4AMgwC2qDSkQwA-AAucYVKo1cpwAA+cHqEnRzXkSii7WBR1B+A+bnUbjcxTQADlVGVKpRqrUAOI0HBQcq0r5jAjXPK3e643n8wkYuA1ABu3DgOIIrPyhR8JSw+NqZMaQ3+gM6Zyex2aqwmU1mp0WWxWY0N6wYkKgTt2tsl1oZOB9cKuNzu5QeIdeh3ebglpSCT3NplpAZhEJd4PTIcRyO6qPJTXwWIW3RJXJEPKN5SFIrFdJtjNLxLgAApWwBKUZfCtVvkE2ui1OHRs4D4d5k6grQeCUBTKAFqLI8ZMdHZpv24yYzOY7L2rd20T2ZnZ7f0jwPBmMvMNyqNjEOfVsLCCRRcmHHuxUmin4VfD0JR3BL0szBWFr0QD55g7T9qx-IsWjfC0G0vZ1llA-BwRzOAkXQBZl2eRBP2aIhiAAnh3nAwDAyZFlcmnbw52pRc4AI-91wvM8twdXczn3N0qCPTZ0NPTcUJODiLhvGVw0jCDoykwQ42fbpXzaHE+2-MRCzNJCU3EsCM3Q6FDJzaCNLxftjW001mn-AzMLQqFfUculLlzPDugIy5iPwUjyMIrDOOaWipz1JiF1UVi-j0kxWzU98cXtHcu08uBUhgGgoDUBLAUnVlqluKBqEUNACAgcoIDzOBgHKHEzCgapiAAbgWCQYDoFB6pgRqRBa-LdRnOBnCSBAKogJM9NWVtau63riBbER6DoLsRiZLyYraVs0o2xScQAbT0GBxr0chaoAXSdXaQRxTtu2bboAAUdGAIIADpUhMCA6HVbaHu6Gq6v0ABGU7-u6drOpxPQsCqgAZWgKh4MGAYB9QOyumqbN-HBbpgnpxpbBQFQJbt5WrJ10eaoA

from query.

TkDodo avatar TkDodo commented on June 8, 2024

okay interestingly, we have test that cover this case already, and they don't fail:

it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
const { data } = useQuery({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => undefined as { wow: boolean } | undefined,
})
expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
})

🤔

from query.

ShacharHarshuv avatar ShacharHarshuv commented on June 8, 2024

I fixed the PR per feedback: #7351

from query.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.