Skip to content

Commit c93b510

Browse files
JoostKthePunderWoman
authored andcommitted
feat(core): allow passing undefined without needing to include it in the type argument of input (#57621)
This commit introduces an overload for `input` to accept `undefined` as initial value if only options are needed to be provided, inferring an input of type `T|undefined`. Prior to this change, the type argument as specified needed to include `|undefined` explicitly even though that isn't necessary when passing options isn't needed. Relates to #53909 PR Close #57621
1 parent c15e72a commit c93b510

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,9 @@ export interface InputDecorator {
985985
export interface InputFunction {
986986
<T>(): InputSignal<T | undefined>;
987987
<T>(initialValue: T, opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
988+
<T>(initialValue: undefined, opts: InputOptionsWithoutTransform<T>): InputSignal<T | undefined>;
988989
<T, TransformT>(initialValue: T, opts: InputOptionsWithTransform<T, TransformT>): InputSignalWithTransform<T, TransformT>;
990+
<T, TransformT>(initialValue: undefined, opts: InputOptionsWithTransform<T | undefined, TransformT>): InputSignalWithTransform<T | undefined, TransformT>;
989991
required: {
990992
<T>(opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
991993
<T, TransformT>(opts: InputOptionsWithTransform<T, TransformT>): InputSignalWithTransform<T, TransformT>;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export interface InputFunction {
5151
<T>(): InputSignal<T | undefined>;
5252
/** Declares an input of type `T` with an explicit initial value. */
5353
<T>(initialValue: T, opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
54+
/** Declares an input of type `T|undefined` without an initial value, but with input options */
55+
<T>(initialValue: undefined, opts: InputOptionsWithoutTransform<T>): InputSignal<T | undefined>;
5456
/**
5557
* Declares an input of type `T` with an initial value and a transform
5658
* function.
@@ -62,6 +64,16 @@ export interface InputFunction {
6264
initialValue: T,
6365
opts: InputOptionsWithTransform<T, TransformT>,
6466
): InputSignalWithTransform<T, TransformT>;
67+
/**
68+
* Declares an input of type `T|undefined` without an initial value and with a transform
69+
* function.
70+
*
71+
* The input accepts values of type `TransformT` and the given
72+
* transform function will transform the value to type `T|undefined`.
73+
*/ <T, TransformT>(
74+
initialValue: undefined,
75+
opts: InputOptionsWithTransform<T | undefined, TransformT>,
76+
): InputSignalWithTransform<T | undefined, TransformT>;
6577

6678
/**
6779
* Initializes a required input.

packages/core/test/authoring/signal_input_signature_test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ export class InputSignatureTest {
6868
/** string | undefined */
6969
withNoInitialValue = input<string>();
7070

71+
/** undefined */
72+
initialValueUndefinedWithoutOptions = input(undefined);
73+
/** undefined */
74+
initialValueUndefinedWithOptions = input(undefined, {});
75+
/** @internal */
76+
__shouldErrorIfInitialValueUndefinedExplicitReadWithoutOptions = input<string>(
77+
// @ts-expect-error
78+
undefined,
79+
);
80+
/** string | undefined, unknown */
81+
initialValueUndefinedWithUntypedTransform = input(undefined, {transform: (bla) => ''});
82+
/** string | undefined, string */
83+
initialValueUndefinedWithTypedTransform = input(undefined, {transform: (bla: string) => ''});
84+
/** string | undefined, string */
85+
initialValueUndefinedExplicitReadWithTransform = input<string, string>(undefined, {
86+
transform: (bla) => '',
87+
});
88+
7189
/** string */
7290
requiredNoInitialValue = input.required<string>();
7391
/** string | undefined */

0 commit comments

Comments
 (0)