Skip to content

Commit 3d72607

Browse files
committed
Improved error output for FormPath
1 parent 39cfedf commit 3d72607

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Fixed type inference for `FormPath` with nested arrays. Error output improved as well.
13+
814
## [2.11.0] - 2024-03-22
915

1016
### Added

src/lib/stringPath.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,32 @@ type BuiltInObjects = Date | Set<unknown> | File;
2424
/**
2525
* Lists all paths in an object as string accessors.
2626
*/
27-
export type FormPath<T extends object, Type = any> = string &
28-
StringPath<T, { filter: 'all'; objAppend: never; path: ''; type: Type }>;
27+
export type FormPath<T extends object, Type = any> = string & T extends any
28+
? StringPath<T, { filter: 'all'; objAppend: never; path: ''; type: Type }>
29+
: never;
2930

3031
/**
3132
* List paths in an object as string accessors, but only with non-objects as accessible properties.
3233
* Similar to the leaves in a node tree, if you look at the object as a tree structure.
3334
*/
34-
export type FormPathLeaves<T extends object, Type = any> = string &
35-
StringPath<T, { filter: 'leaves'; objAppend: never; path: ''; type: Type }>;
35+
export type FormPathLeaves<T extends object, Type = any> = string & T extends any
36+
? StringPath<T, { filter: 'leaves'; objAppend: never; path: ''; type: Type }>
37+
: never;
3638

3739
/**
3840
* List paths in an object as string accessors, but only with non-objects as accessible properties.
3941
* Also includes the _errors field for objects and arrays.
4042
*/
41-
export type FormPathLeavesWithErrors<T extends object, Type = any> = string &
42-
StringPath<T, { filter: 'leaves'; objAppend: '_errors'; path: ''; type: Type }>;
43+
export type FormPathLeavesWithErrors<T extends object, Type = any> = string & T extends any
44+
? StringPath<T, { filter: 'leaves'; objAppend: '_errors'; path: ''; type: Type }>
45+
: never;
4346

4447
/**
4548
* List all arrays in an object as string accessors.
4649
*/
47-
export type FormPathArrays<T extends object, Type = any> = string &
48-
StringPath<T, { filter: 'arrays'; objAppend: never; path: ''; type: Type }>;
50+
export type FormPathArrays<T extends object, Type = any> = string & T extends any
51+
? StringPath<T, { filter: 'arrays'; objAppend: never; path: ''; type: Type }>
52+
: never;
4953

5054
type Concat<
5155
Path extends string,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ test('FormPath with type narrowing, arrays', () => {
324324
const i2: NameArrays = 'tags';
325325
});
326326

327+
test('FormPath with type narrowing, arrays 2', () => {
328+
type NameArrays = FormPath<FormData, string[]>;
329+
330+
const t1: NameArrays = 'field1';
331+
const t2: NameArrays = 'field3.nestedArray[3].innerArray';
332+
333+
// @ts-expect-error incorrect path
334+
const i1: NameArrays = 'field1[2].id';
335+
// @ts-expect-error incorrect path
336+
const i2: NameArrays = 'field3.nestedArray[2]';
337+
});
338+
327339
test('FormPath with type narrowing, union', () => {
328340
type NameArrays = FormPath<Obj, string | number>;
329341

0 commit comments

Comments
 (0)