Skip to content

docs(angular): use type annotations instead of casting #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions docs/framework/angular/guides/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ TanStack Form supports arrays as values in a form, including sub-object values i
To use an array, you can use `field.api.state.value` on an array value:

```angular-ts
interface Person {
name: string
age: number
}

@Component({
selector: 'app-root',
standalone: true,
Expand All @@ -25,10 +30,10 @@ To use an array, you can use `field.api.state.value` on an array value:
`,
})
export class AppComponent {
defaultPeople: { people: Array<Person> } = { people: [] }

form = injectForm({
defaultValues: {
people: [] as Array<{ name: string; age: number }>,
},
defaultValues: this.defaultPeople,
onSubmit({ value }) {
alert(JSON.stringify(value))
},
Expand Down Expand Up @@ -93,6 +98,11 @@ export class AppComponent {
## Full Example

```angular-ts
interface Person {
name: string
age: number
}

@Component({
selector: 'app-root',
standalone: true,
Expand Down Expand Up @@ -135,11 +145,10 @@ export class AppComponent {
})
export class AppComponent {
defaultPerson = { name: '', age: 0 }
defaultPeople: { people: Array<Person> } = { people: [] }

form = injectForm({
defaultValues: {
people: [] as Array<{ name: string; age: number }>,
},
defaultValues: this.defaultPeople,
onSubmit({ value }) {
alert(JSON.stringify(value))
},
Expand Down
15 changes: 8 additions & 7 deletions docs/framework/angular/guides/basic-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ When working with array fields, you can use the fields `pushValue`, `removeValue
Example:

```angular-ts
interface Hobby {
name: string
description: string
yearsOfExperience: number
}

@Component({
selector: 'app-root',
standalone: true,
Expand Down Expand Up @@ -311,18 +317,13 @@ export class AppComponent {
description: '',
yearsOfExperience: 0,
}
defaultHobbies: { hobbies: Array<Hobby> } = { hobbies: [] }

getHobbyName = (idx: number) => `hobbies[${idx}].name` as const;
getHobbyDesc = (idx: number) => `hobbies[${idx}].description` as const;

form = injectForm({
defaultValues: {
hobbies: [] as Array<{
name: string
description: string
yearsOfExperience: number
}>,
},
defaultValues: this.defaultHobbies,
onSubmit({ value }) {
alert(JSON.stringify(value))
},
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ It's worth mentioning that our `errors` array and the `errorMap` matches the typ
>
<!-- ... -->
<!-- errorMap.onChange is type `{isOldEnough: false} | undefined` -->
<!-- meta.errors is type `Array<{isOldEnough: false} | undefined>` -->
<!-- meta.errors is type `Array<{isOldEnough: false} | undefined>` -->
@if (!age.api.state.meta.errorMap['onChange']?.isOldEnough) {
<em role="alert">The user is not old enough</em>
}
Expand Down
12 changes: 8 additions & 4 deletions examples/angular/array/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Component } from '@angular/core'
import { TanStackField, injectForm, injectStore } from '@tanstack/angular-form'

interface Person {
name: string
age: number
}

@Component({
selector: 'app-root',
standalone: true,
Expand Down Expand Up @@ -42,12 +47,11 @@ import { TanStackField, injectForm, injectStore } from '@tanstack/angular-form'
`,
})
export class AppComponent {
defaultPerson = { name: '', age: 0 }
defaultPerson: Person = { name: '', age: 0 }
defaultPeople: { people: Array<Person> } = { people: [] }

form = injectForm({
defaultValues: {
people: [] as Array<{ name: string; age: number }>,
},
defaultValues: this.defaultPeople,
onSubmit({ value }) {
alert(JSON.stringify(value))
},
Expand Down
Loading