Skip to content

Commit 1f06f98

Browse files
committed
FormPathLeaves didn't handle fields with type unknown.
1 parent b53adfb commit 1f06f98

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- `defaults` didn't generate correct `SuperValidated` data, making `superForm` confused. Also fixed type signature and removed the `jsonSchema` option that wasn't applicable.
1919
- A successful `PageData` result from `invalidateAll` was overwritten by the `ActionData` result.
2020
- Using `goto` in events didn't work when the target page redirected.
21+
- `FormPathLeaves` didn't handle fields with type `unknown`.
2122

2223
## [2.5.0] - 2024-02-21
2324

src/lib/stringPath.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ type StringPath<
149149
>
150150
: never)
151151
:
152-
| If<Options, 'filter', 'all', Concat<Options['path'], K>, never, T[K]>
152+
| If<
153+
Options,
154+
'filter',
155+
'all',
156+
Concat<Options['path'], K>,
157+
unknown extends T[K] ? Concat<Options['path'], K> : never,
158+
T[K]
159+
>
153160
| StringPath<
154161
NonNullable<T[K]>,
155162
{

src/tests/legacy/paths.test-d.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,16 @@ test('setError paths', async () => {
300300

301301
test('FormPath with type narrowing, simple types', () => {
302302
type Numbers = FormPath<Obj, number>;
303+
type Nevers = FormPath<Obj, File>;
303304

304305
const t1: Numbers = 'points';
305306
const t2: Numbers = 'tags[2].id';
306307
const t3: Numbers = 'tags[2].parents[3]';
307308

308309
// @ts-expect-error incorrect path
309-
const nested: Numbers = 'name';
310+
const f1: Numbers = 'name';
311+
// @ts-expect-error incorrect path
312+
const f2: Nevers = 'nope';
310313
});
311314

312315
test('FormPath with type narrowing, arrays', () => {
@@ -320,6 +323,22 @@ test('FormPath with type narrowing, arrays', () => {
320323
const i2: NameArrays = 'tags';
321324
});
322325

326+
test('FormPath with unknown type', () => {
327+
const schema = z.object({
328+
name: z.string(),
329+
age: z.number(),
330+
homePlanet: z.unknown()
331+
});
332+
333+
type FP = FormPath<z.infer<typeof schema>>;
334+
type FPL = FormPathLeaves<z.infer<typeof schema>>;
335+
type FPA = FormPathArrays<z.infer<typeof schema>>;
336+
337+
const t1: FP = 'homePlanet';
338+
const t2: FPL = 'homePlanet';
339+
const t3: FPA = 'homePlanet';
340+
});
341+
323342
///////////////////////////////////////////////////
324343

325344
// Recursive schema test

0 commit comments

Comments
 (0)