mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
This commit adds the last remaining piece for signal input
type-checking. Bound values to signal inputs are already checked
properly at this point, but inference of generic directive/component
types through their inputs is not implemented.
This commit fixes this. To achieve this, there are a couple of potential
solutions. The generics of a directive are inferred based on input
value expressions using a so-called type constructor. The constructor
looks something like this:
```
const _ctor = <T>(v: Pick<Dir<T>, 'input1', 'input2'>) => Dir<T>;
_ctor({input1: expr1, input2: expr2});
```
This works very well for non-signal inputs where the class member is
directly holding the input values. For signal inputs, this does NOT
work because the class member will actually hold the `InputSignal`
instance. There are a couple of solutions to this:
1. Calling `_ctor` with an `InputSignal<typeof value>`
2. Converting the `_ctor` input signal fields to their write types
(unwrapping the input signals).
We've decided to go with the second option as TypeScript is very
sensitive with assignments and its checks. i.e. co-variance,
contravariance or bivariance. Semantically it makes more sense to unwrap
the input signal "write type" directly and "assign to it". This is safer
and conceptually also easier to follow. A type constructor continues to
only receive the "expresison values". This simplifies code as well.
It's worth noting that the unwrapping as per option 2 also comes at a
cost. We need to be able to generate imports in type constructors. This
was not possible until the previous commit because inline type constructors
did not have an associated type-check block `Environment` and we were
missing access to expression translation and correct import generation.
Overall, solution 2 is now implemented as works as expected. This commit
adds additional unit tests to ensure this.
PR Close #53521
|
||
|---|---|---|
| .. | ||
| i18n | ||
| instructions | ||
| interfaces | ||
| ivy | ||
| jit | ||
| styling_next | ||
| util | ||
| BUILD.bazel | ||
| change_detection_spec.ts | ||
| component_ref_spec.ts | ||
| deps_tracker_spec.ts | ||
| di_spec.ts | ||
| es2015-tsconfig.json | ||
| global_utils_spec.ts | ||
| i18n_debug_spec.ts | ||
| imported_renderer2.ts | ||
| instructions_spec.ts | ||
| integration_spec.ts | ||
| is_shape_of.ts | ||
| is_shape_of_spec.ts | ||
| jit_environment_spec.ts | ||
| list_reconciliation_spec.ts | ||
| load_domino.ts | ||
| matchers.ts | ||
| matchers_spec.ts | ||
| metadata_spec.ts | ||
| multi_map_spec.ts | ||
| node_selector_matcher_spec.ts | ||
| providers_helper.ts | ||
| providers_spec.ts | ||
| query_spec.ts | ||
| reactivity_spec.ts | ||
| testing_spec.ts | ||
| utils.ts | ||
| view_fixture.ts | ||
| view_utils_spec.ts | ||