Skip to content

Commit

Permalink
docs: document a fix to the issues with TypeScript types in ng arrays (
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Mar 23, 2024
1 parent d203703 commit c694e4c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
34 changes: 30 additions & 4 deletions docs/framework/angular/guides/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ This will generate the mapped JSX every time you run `pushValue` on `field`:
Finally, you can use a subfield like so:

```html
<ng-container
<ng-container
[tanstackField]="form"
[name]="'people[' + $index + '].name'"
[name]="getPeopleName($index)"
#person="field"
>
<div>
Expand All @@ -66,6 +66,29 @@ Finally, you can use a subfield like so:
</ng-container>
```

Where `getPeopleName` is a method on the class like so

```typescript
export class AppComponent {
getPeopleName = (idx: number) => `people[${idx}].name` as const;

// ...
}
```

> While it's unfortunate that you need to use a function to get the field name, it's a requirement for how our strict TypeScript types work.
>
> See, if we did the following:
> ```html
> <ng-container [tanstackField]="form" [name]="'people[' + $index + '].name'"></ng-container>
> ```
>
> We'd be running into a TypeScript issue where `"one" + "two"` is `string` rather than the required `"onetwo"` type
>
> Moreover, while Angular supports template literals in the template, they may not contain dynamic interpolation within them, such as our `$index` argument.
> It's possible that we've missed something! If you can think of a better fix for this problem, [drop us a line on our GitHub discussions](https://github.com/TanStack/form/discussions).
## Full Example

```typescript
Expand All @@ -81,7 +104,7 @@ Finally, you can use a subfield like so:
@for (_ of people.api.state.value; track $index) {
<ng-container
[tanstackField]="form"
[name]="'people[' + $index + '].name'"
[name]="getPeopleName($index)"
#person="field"
>
<div>
Expand Down Expand Up @@ -121,6 +144,9 @@ export class AppComponent {
},
})


getPeopleName = (idx: number) => `people[${idx}].name` as const;

canSubmit = injectStore(this.form, (state) => state.canSubmit)
isSubmitting = injectStore(this.form, (state) => state.isSubmitting)

Expand All @@ -130,4 +156,4 @@ export class AppComponent {
void this.form.handleSubmit()
}
}
```
```
4 changes: 3 additions & 1 deletion examples/angular/array/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TanStackField, injectForm, injectStore } from '@tanstack/angular-form'
@for (_ of people.api.state.value; track $index) {
<ng-container
[tanstackField]="form"
[name]="'people[' + $index + '].name'"
[name]="getPeopleName($index)"
#person="field"
>
<div>
Expand Down Expand Up @@ -53,6 +53,8 @@ export class AppComponent {
},
})

getPeopleName = (idx: number) => `people[${idx}].name` as const

canSubmit = injectStore(this.form, (state) => state.canSubmit)
isSubmitting = injectStore(this.form, (state) => state.isSubmitting)

Expand Down

0 comments on commit c694e4c

Please sign in to comment.