-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Location type should have template for unknown for state #930
Comments
I thinks it's a good idea |
Passing a generic for location.state used to give a generic (see history/api-reference) The generic was removed in this commit.
|
@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:
interface IComponentProps extends IBlablaProps, IWithRouter<{ fooBar: string }> { }
const location = useLocation() as ILocationState<{ fooBar: string }>; |
Any typesafe use of Location requires casting the unknown type into something else (casting is usually a code smell). My primary use for this is in React Router with the
useLocation
hook when accessing the state property.It seems like a template variable would fix this up. Perhaps something like this would work.
The text was updated successfully, but these errors were encountered: