GithubHelp home page GithubHelp logo

Comments (3)

mkerkstra avatar mkerkstra commented on April 28, 2024 2

Passing a generic for location.state used to give a generic (see history/api-reference)
Screen Shot 2022-02-10 at 11 19 34 AM

The generic was removed in this commit.

location.state is like location.search, there's no way for a type system to do its job with a value that isn't controlled by the code. Similarly, the DOM type for URLSearchParams is not generic. And the built-in types for window.history.state is any without a generic. The only way to type check location.state or location.search or really any value on location.* is to do it at runtime and then get type hints from using type guards.

from history.

luotongzhou avatar luotongzhou commented on April 28, 2024

I thinks it's a good idea

from history.

XavierDefontaine avatar XavierDefontaine commented on April 28, 2024

@elijahrdorman Seconded. This temporarily fixed the problem for me with the Class hoc:

export interface ILocationState<Type> extends Omit<Location, 'state'> { state: any | Type };

export interface IWithRouter<Type = any> {  //TODO: make this a mandatory Type so it's easier to keep track of State between components.
  router: {
    location: ILocationState<Type>;
    navigate: NavigateFunction;
    params: Readonly<Params<string>>;
  }
}
};

Usage:

  • Class:
interface IComponentProps extends IBlablaProps, IWithRouter<{ fooBar: string }> { }
  • Functional:
const location = useLocation() as ILocationState<{ fooBar: string }>;

from history.

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.