GithubHelp home page GithubHelp logo

Comments (3)

garyhuntddn avatar garyhuntddn commented on September 24, 2024

Ha ha - I can see what you’d like it to do, but that’ll never be possible given that typescript is a facade on top of JavaScript - you’re telling it what you expect, but in no way is the JavaScript contracted to do that.

the general use case of swr is to call fetch, and most assume that the json they then attempt to cast into a type will conform to that type - but that won’t be the case on many occasions. Therefore always treat swr as an api boundary and never trust the results to be what you hope them to be.

in this particular example you’ve simply used the same cache key (‘test’) for multiple things and any library would suffer the same problem.

from swr.

tjallingt avatar tjallingt commented on September 24, 2024

But I would expect the fetcher to, implicitly, be part of the cache key. So that "test" + "fetchOne" is cached in a different slot than "test" + "fetchTwo". That would also fix the error with the types.

from swr.

garyhuntddn avatar garyhuntddn commented on September 24, 2024

I've never seen it done - but there's no harm in you creating your own standard...

const { data } = useSWR( [ "test", fetchOne ], fetchOne);

and

const { data } = useSWR( [ "test", fetchTwo ], fetchTwo);

Would then include the fetcher in the key - but once again - remember that in real world use - what gets returned from a cache of any kind might not be what you expect - so you should code more defensively and expect the unexpected.

from swr.

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.