Skip to content

Make input signal values Readonly types by default (so child properties on an object can't be changed) #63438

@jpike88

Description

@jpike88

Which @angular/* package(s) are relevant/related to the feature request?

@angular/core

Description

inputs signals are supposed to be readonly. but if the input is an object (like an options object), that readonly guarantee isn't pass down to the child properties.

myInput = input<{
    childProperty: string
}>();

ngOnInit(){
	 myInput.set({childProperty: 'hello'}); // this isn't allowed...
     myInput().childProperty = 'hello';   // but this is allowed!
}

I don't understand why this is the default. surely the assumption should be that transforming child properties against an input signal object value isn't desirable as it bypasses change detection.

In other words, I don't think that I should have to do this every time:

myInput = input<Readonly<{
    childProperty: string
}>>();

ngOnInit(){
     myInput().childProperty = 'hello'   // this is not allowed anymore
}

Proposed solution

Ensure that child properties on input signal object values are readonly, to be consistent with the signal itself being readonly.

Alternatives considered

Using typescript's built-in Readonly type to wrap the signal's generic each time (which is a footgun because you have to remember to do it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions