Skip to content

Conversation

@crisbeto
Copy link
Member

Currently it's easy to make a mistake when accessing properties on SimpleChanges, because the keys aren't typed. These changes add an optional generic to the interface so that users can get a compilation error if they make a typo.

A few things to note:

  1. The generic argument is optional and we revert to the old behavior if one isn't passed for backwards compatibility.
  2. All of the keys are optional, because they aren't guaranteed to be present for any ngOnChanges invocation.
  3. We unwrap the values of input signals to match the behavior at runtime.

Fixes #17560.

Moves the `SimpleChange` and lifecycle hook interfaces so that they can import other symbols from `core`.
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: minor This PR is targeted for the next minor release labels Oct 20, 2025
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Oct 20, 2025
@ngbot ngbot bot added this to the Backlog milestone Oct 20, 2025
Currently it's easy to make a mistake when accessing properties on `SimpleChanges`, because the keys aren't typed. These changes add an optional generic to the interface so that users can get a compilation error if they make a typo.

A few things to note:
1. The generic argument is optional and we revert to the old behavior if one isn't passed for backwards compatibility.
2. All of the keys are optional, because they aren't guaranteed to be present for any `ngOnChanges` invocation.
3. We unwrap the values of input signals to match the behavior at runtime.

Fixes angular#17560.
@crisbeto crisbeto requested a review from devversion October 20, 2025 13:13
@crisbeto crisbeto marked this pull request as ready for review October 20, 2025 13:13
@pullapprove pullapprove bot requested a review from kirjs October 20, 2025 13:13
@crisbeto crisbeto modified the milestones: Backlog, v21 Candidate Oct 20, 2025
export type SimpleChanges<T = unknown> = T extends object
? {
[Key in keyof T]?: SimpleChange<
T[Key] extends {[ɵINPUT_SIGNAL_BRAND_READ_TYPE]: infer V} ? V : T[Key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know we have a helper for this already? (e.g. for type checking?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK we don't, I only found the function that asserts if a type is a writable signal. Initially I wanted to use the public InputSignal type, but it would've gotten complicated since we have some variations like InputSignalWithTransform.

@pullapprove pullapprove bot requested a review from devversion October 20, 2025 14:00
@crisbeto
Copy link
Member Author

Passing TGP after some client app cleanups.

Copy link
Member

@devversion devversion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

expect(ngModel.control.disabled).toEqual(false);

ngModel.ngOnChanges({isDisabled: new SimpleChange('', false, false)});
ngModel.ngOnChanges({isDisabled: new SimpleChange('' as any, false, false)});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to cast those?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because the types for the previous and current value don't match. I think it's testing something that isn't officially supported through types.

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 20, 2025
@crisbeto

This comment was marked as outdated.

@crisbeto crisbeto removed the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 20, 2025
thePunderWoman pushed a commit that referenced this pull request Oct 20, 2025
Currently it's easy to make a mistake when accessing properties on `SimpleChanges`, because the keys aren't typed. These changes add an optional generic to the interface so that users can get a compilation error if they make a typo.

A few things to note:
1. The generic argument is optional and we revert to the old behavior if one isn't passed for backwards compatibility.
2. All of the keys are optional, because they aren't guaranteed to be present for any `ngOnChanges` invocation.
3. We unwrap the values of input signals to match the behavior at runtime.

Fixes #17560.

PR Close #64535
@thePunderWoman
Copy link
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support generic SimpleChange<T> for use in ngOnChanges

4 participants