feat(core): make SimpleChanges generic#64535
Conversation
Moves the `SimpleChange` and lifecycle hook interfaces so that they can import other symbols from `core`.
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.
c691b52 to
ae7eda8
Compare
| 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] |
There was a problem hiding this comment.
Do you know we have a helper for this already? (e.g. for type checking?)
There was a problem hiding this comment.
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.
|
Passing TGP after some client app cleanups. |
| expect(ngModel.control.disabled).toEqual(false); | ||
|
|
||
| ngModel.ngOnChanges({isDisabled: new SimpleChange('', false, false)}); | ||
| ngModel.ngOnChanges({isDisabled: new SimpleChange('' as any, false, false)}); |
There was a problem hiding this comment.
Why do you need to cast those?
There was a problem hiding this comment.
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.
This comment was marked as outdated.
This comment was marked as outdated.
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
|
This PR was merged into the repository. The changes were merged into the following branches:
|
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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:
ngOnChangesinvocation.Fixes #17560.