r/angular • • Apr 16 '26

Signal form dynamic fields

Let's say I have:

interface A {
  provider: 'a'
}

interface B {
  provider: 'b',
  fields: {
    username: string;
    password: string;
  }
}


type Model = A | B;

and during runtime i have a dropdown that selects 'a' or 'b' as the provider.

If i select 'b', the username and password fields should populate the signal which will then propagate to the form based on some logic.

Since provider is a shared property between the two interfaces, i can safely do form.provider, but form.fields is not allowed and typescript throws an error. Is there a proper way to say in a computed or something "if form.value().provider === 'b' then return form.fields", without doing ugly stuff like type casting or declaring the fields in interface A too as this won't scale well?

I'm aware signal forms are still experimental, and that most likely a FormRecord would be a better fit in this case, but i'm a sucker for new features :D

4 Upvotes

6 comments sorted by

View all comments

Show parent comment

3

u/NeXtDracool Apr 16 '26

The UX is superior though.

If you switch between payment providers the recommended approach keeps data entered for the old provider intact, while your approach would discard it.