Skip to content

Commit 1ab654c

Browse files
cexbrayatalxhub
authored andcommitted
fix(core): allow explicit read generic with signal input transforms
Using explicit single generic arguments with transforms (for example, input<boolean>(false, {transform: booleanAttribute})) previously failed overload resolution. Before this fix, type-checking produced: ```` ✘ [ERROR] TS2769: No overload matches this call. Overload 1 of 5, '(initialValue: boolean, opts?: InputOptionsWithoutTransform<boolean> | undefined): InputSignal<boolean>', gave the following error. Type '(value: unknown) => boolean' is not assignable to type 'undefined'. Overload 2 of 5, '(initialValue: undefined, opts: InputOptionsWithoutTransform<boolean>): InputSignal<boolean | undefined>', gave the following error. Argument of type 'true' is not assignable to parameter of type 'undefined'. [plugin angular-compiler] ``` This change adds specialized overloads for explicit read generics.
1 parent a4145de commit 1ab654c

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

goldens/public-api/core/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,8 @@ export interface InputFunction {
10251025
<T>(initialValue: undefined, opts: InputOptionsWithoutTransform<T>): InputSignal<T | undefined>;
10261026
<T, TransformT>(initialValue: T, opts: InputOptionsWithTransform<T, TransformT>): InputSignalWithTransform<T, TransformT>;
10271027
<T, TransformT>(initialValue: undefined, opts: InputOptionsWithTransform<T | undefined, TransformT>): InputSignalWithTransform<T | undefined, TransformT>;
1028+
<T>(initialValue: T, opts: InputOptionsWithTransform<T, unknown>): InputSignalWithTransform<T, T>;
1029+
<T>(initialValue: undefined, opts: InputOptionsWithTransform<T | undefined, unknown>): InputSignalWithTransform<T | undefined, T | undefined>;
10281030
required: {
10291031
<T>(opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
10301032
<T, TransformT>(opts: InputOptionsWithTransform<T, TransformT>): InputSignalWithTransform<T, TransformT>;

packages/core/src/authoring/input/input.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ export interface InputFunction {
7777
initialValue: undefined,
7878
opts: InputOptionsWithTransform<T | undefined, TransformT>,
7979
): InputSignalWithTransform<T | undefined, TransformT>;
80+
/**
81+
* Declares an input of type `T` with an initial value and a transform function
82+
* that accepts values of the same type.
83+
*/
84+
<T>(initialValue: T, opts: InputOptionsWithTransform<T, unknown>): InputSignalWithTransform<T, T>;
85+
/**
86+
* Declares an input of type `T|undefined` without an initial value and with a transform
87+
* function that accepts values of the same type.
88+
*/
89+
<T>(
90+
initialValue: undefined,
91+
opts: InputOptionsWithTransform<T | undefined, unknown>,
92+
): InputSignalWithTransform<T | undefined, T | undefined>;
8093

8194
/**
8295
* Initializes a required input.

packages/core/test/authoring/signal_input_signature_test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* the resulting types match our expectations (via comments asserting the `.d.ts`).
1313
*/
1414

15-
import {input} from '../../src/core';
15+
import {booleanAttribute, input, numberAttribute} from '../../src/core';
1616
// import preserved to simplify `.d.ts` emit and simplify the `type_tester` logic.
1717
// tslint:disable-next-line no-duplicate-imports
1818
import {InputSignal, InputSignalWithTransform} from '../../src/core';
@@ -102,6 +102,19 @@ export class InputSignatureTest {
102102
transform: (v: string | boolean) => '',
103103
});
104104

105+
/** boolean, boolean */
106+
explicitReadWithBooleanAttributeTransform = input<boolean>(false, {transform: booleanAttribute});
107+
/** number, number */
108+
explicitReadWithNumberAttributeTransform = input<number>(0, {transform: numberAttribute});
109+
/** boolean | undefined, boolean | undefined */
110+
explicitReadWithUndefinedInitialBooleanAttributeTransform = input<boolean>(undefined, {
111+
transform: booleanAttribute,
112+
});
113+
/** number | undefined, number | undefined */
114+
explicitReadWithUndefinedInitialNumberAttributeTransform = input<number>(undefined, {
115+
transform: numberAttribute,
116+
});
117+
105118
/** string, string | boolean */
106119
requiredWithTransformInferenceNoExplicitGeneric = input.required({
107120
transform: (v: string | boolean) => '',

0 commit comments

Comments
 (0)