From f5d565dd4b610e440ee1c1e842e8e4117008d89f Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Thu, 1 Feb 2024 11:43:17 +1300 Subject: [PATCH 1/3] fix: check matcher test types (#1057) --- package.json | 2 +- test/{Matcher.test.js => Matcher.test.ts} | 26 +++++++++++++++++------ test/tsconfig.json | 4 ++++ 3 files changed, 25 insertions(+), 7 deletions(-) rename test/{Matcher.test.js => Matcher.test.ts} (94%) create mode 100644 test/tsconfig.json diff --git a/package.json b/package.json index db27c21d..a4ae6188 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "release": "4c release --conventional-commits", "tdd": "jest --watch", "test": "yarn lint && yarn test:ts && yarn testonly -- --coverage", - "test:ts": "dtslint --expectOnly types && yarn tsc --noEmit", + "test:ts": "dtslint --expectOnly types && yarn tsc --noEmit && yarn tsc --noEmit -p test", "testonly": "jest --runInBand --verbose", "docs": "yarn --cwd www start" }, diff --git a/test/Matcher.test.js b/test/Matcher.test.ts similarity index 94% rename from test/Matcher.test.js rename to test/Matcher.test.ts index d66ec5af..66d02a34 100644 --- a/test/Matcher.test.js +++ b/test/Matcher.test.ts @@ -1,4 +1,9 @@ import Matcher from '../src/Matcher'; +import { + type IsActiveOptions, + type LocationDescriptorObject, + Match, +} from '../src/typeUtils'; describe('Matcher', () => { describe('route hierarchies', () => { @@ -32,11 +37,11 @@ describe('Matcher', () => { ['nested matching', '/foo/bar', [0, 1]], ['route fallthrough', '/foo/baz', [2]], ].forEach(([scenario, pathname, expectedRouteIndices]) => { - describe(scenario, () => { + describe(scenario as string, () => { it('should be supported', () => { expect( matcher.match({ - pathname, + pathname: pathname as string, }), ).toMatchObject({ routeIndices: expectedRouteIndices, @@ -265,7 +270,7 @@ describe('Matcher', () => { it(`should match ${scenario}`, () => { expect( matcher.match({ - pathname, + pathname: pathname as string, }), ).toMatchObject({ routeIndices: expectedRouteIndices, @@ -350,7 +355,7 @@ describe('Matcher', () => { }); describe('#joinPaths', () => { - const matcher = new Matcher(); + const matcher = new Matcher([]); [ ['no extra slashes', '/foo', 'bar'], @@ -359,6 +364,7 @@ describe('Matcher', () => { ['slashes everywhere', '/foo/', '/bar'], ].forEach(([scenario, basePath, path]) => { it(`should support ${scenario}`, () => { + // @ts-ignore expect(matcher.joinPaths(basePath, path)).toBe('/foo/bar'); }); }); @@ -415,7 +421,11 @@ describe('Matcher', () => { ].forEach(([scenario, matchLocation, location, options]) => { it(`should be active on ${scenario}`, () => { expect( - matcher.isActive({ location: matchLocation }, location, options), + matcher.isActive( + { location: matchLocation } as any as Match, + location as LocationDescriptorObject, + options as IsActiveOptions, + ), ).toBe(true); }); }); @@ -459,7 +469,11 @@ describe('Matcher', () => { ].forEach(([scenario, matchLocation, location, options]) => { it(`should not be active on ${scenario}`, () => { expect( - matcher.isActive({ location: matchLocation }, location, options), + matcher.isActive( + { location: matchLocation } as any as Match, + location as LocationDescriptorObject, + options as IsActiveOptions, + ), ).toBe(false); }); }); diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 00000000..e516b7fa --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../tsconfig.json", + "include": ["**/*.ts"] +} From 2ce1eb660bb39edf28b07b0c38fe4510c74ef859 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Thu, 1 Feb 2024 12:07:01 +1300 Subject: [PATCH 2/3] fix: reproduce problem with params typing (#1057) --- test/Matcher.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/Matcher.test.ts b/test/Matcher.test.ts index 66d02a34..a887e06c 100644 --- a/test/Matcher.test.ts +++ b/test/Matcher.test.ts @@ -220,11 +220,15 @@ describe('Matcher', () => { }, ]); - expect( - matcher.match({ - pathname: '/a', - }), - ).toEqual({ + const missingMatch = matcher.match({ pathname: '/a' }); + + if (missingMatch != null && 0 in missingMatch.params) { + // FIXME: This type is wrong. It should be string | undefined. + const param: string = missingMatch.params[0]; + expect(param).toBeUndefined(); + } + + expect(missingMatch).toEqual({ routeIndices: [0, 0], routeParams: [{ foo: 'a' }, { 0: undefined }], params: { From ebde85811652d6041289b9ae6bc8ffaaf08598c9 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Thu, 1 Feb 2024 12:18:55 +1300 Subject: [PATCH 3/3] fix: optional params may be undefined (#1057) --- src/typeUtils.ts | 4 ++-- test/Matcher.test.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/typeUtils.ts b/src/typeUtils.ts index b0da5dd7..66170644 100644 --- a/src/typeUtils.ts +++ b/src/typeUtils.ts @@ -39,11 +39,11 @@ type Omit = Pick>; // RESOLVE_MATCH: '@@found/RESOLVE_MATCH'; // }; -export type Params = Record; +export type Params = Record; export type ParamsDescriptor = Record< string, - string | number | boolean | Record + string | number | boolean | Record | undefined >; // These need to be interfaces to avoid circular reference issues. diff --git a/test/Matcher.test.ts b/test/Matcher.test.ts index a887e06c..94c97763 100644 --- a/test/Matcher.test.ts +++ b/test/Matcher.test.ts @@ -223,8 +223,7 @@ describe('Matcher', () => { const missingMatch = matcher.match({ pathname: '/a' }); if (missingMatch != null && 0 in missingMatch.params) { - // FIXME: This type is wrong. It should be string | undefined. - const param: string = missingMatch.params[0]; + const param: string | undefined = missingMatch.params[0]; expect(param).toBeUndefined(); }