[@types/react-router] Added undefined union type handling for useParams. (#43213)

This commit is contained in:
Pawel 2020-04-01 01:15:26 +02:00 committed by GitHub
parent be9dcd06bc
commit 314ba85888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -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 } = {}>(

View File

@ -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;