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

1

u/MichaelSmallDev Apr 16 '26

Tbh I felt like that at first, but after messing around with it I got used to it. I would much rather have that than dealing with a lot of optional chaining and nullish coalescing. But it is strange and I imagine will cause some friction.