mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
[@types/react-router] Added undefined union type handling for useParams. (#43213)
This commit is contained in:
parent
be9dcd06bc
commit
314ba85888
3
types/react-router/index.d.ts
vendored
3
types/react-router/index.d.ts
vendored
@ -20,6 +20,7 @@
|
||||
// Wesley Tsai <https://github.com/wezleytsai>
|
||||
// Sebastian Silbermann <https://github.com/eps1lon>
|
||||
// Nicholas Hehr <https://github.com/HipsterBrown>
|
||||
// Pawel Fajfer <https://github.com/pawfa>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
@ -161,7 +162,7 @@ export function useHistory<HistoryLocationState = H.LocationState>(): H.History<
|
||||
|
||||
export function useLocation<S = H.LocationState>(): H.Location<S>;
|
||||
|
||||
export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): { [p in keyof Params]: string };
|
||||
export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): { [p in keyof Params]: keyof Params[p] extends undefined ? string | undefined : string };
|
||||
|
||||
export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(): match<Params>;
|
||||
export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(
|
||||
|
||||
@ -5,6 +5,11 @@ interface Params {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface OptionalParams {
|
||||
id?: string;
|
||||
s: string | undefined;
|
||||
}
|
||||
|
||||
interface LocationState {
|
||||
s: string;
|
||||
}
|
||||
@ -14,6 +19,8 @@ const HooksTest: React.FC = () => {
|
||||
const location = useLocation<LocationState>();
|
||||
const { id } = useParams();
|
||||
const params = useParams<Params>();
|
||||
// $ExpectType { id?: string | undefined; s: string | undefined; }
|
||||
const optionalParams = useParams<OptionalParams>();
|
||||
// $ExpectType match<Params> | null
|
||||
const match1 = useRouteMatch<Params>('/:id');
|
||||
// $ExpectType match<Params> | null
|
||||
@ -27,6 +34,8 @@ const HooksTest: React.FC = () => {
|
||||
location.state.s;
|
||||
id && id.replace;
|
||||
params.id.replace;
|
||||
optionalParams.id && optionalParams.id.replace;
|
||||
optionalParams.s && optionalParams.s.replace;
|
||||
match1 && match1.params.id.replace;
|
||||
match2 && match2.params.id.replace;
|
||||
match3 && match3.params.id.replace;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user