Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(core): allow passing undefined without needing to include it i… #57621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

JoostK
Copy link
Member

@JoostK JoostK commented Sep 1, 2024

…n the type argument of input

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.

Note: this is an experiment to determine if such change could impact existing code, it is yet to be determined if this is the direction we want to take.

Relates to #53909

@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Sep 1, 2024
@ngbot ngbot bot added this to the Backlog milestone Sep 1, 2024
@@ -51,6 +51,8 @@ export interface InputFunction {
<T>(): InputSignal<T | undefined>;
/** Declares an input of type `T` with an explicit initial value. */
<T>(initialValue: T, opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
/** Declares an input of type `T|undefined` without an initial value, but with input options */
<T>(initialValue: undefined, opts: InputOptionsWithoutTransform<T>): InputSignal<T | undefined>;
Copy link
Member Author

Choose a reason for hiding this comment

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

I decided to make opts required if an explicit undefined is passed, making input<string>(undefined) a type error, with the intention that that should always be achieved using just input<string>().

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if that is not confusing. TS error messages are not super great, but I like the incentive to encourage the shorthand.

…n the type argument of `input`

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 angular#53909
@JoostK JoostK force-pushed the core/input-implicit-undefined branch from 3c2c0fb to ff25cab Compare September 1, 2024 18:46
@ArielGueta
Copy link

ArielGueta commented Sep 2, 2024

The most difficult thing in the migration is if you have an input:

@Input() foo: string;

and you migrate it to signal input:

foo = input<string | undefined>(undefined)

The type is now "broken" because the consumers of this input supports only string and not expect to get undefined. We have many of these in our application and this is the reason we are not able to migrate currently.

@JoostK
Copy link
Member Author

JoostK commented Sep 2, 2024

The most difficult thing in the migration is if you have an input:

@Input() foo: string;

and you migrate it to signal input:

foo = input<string | undefined>(undefined)

The type is now "broken" because the consumers of this input supports only string and not expect to get undefined. We have many of these in our application and this is the reason we are not able to migrate currently.

That's unrelated to the changes here. The original input declaration is unsafe as the input may not be assigned, making it undefined. input.required avoids the introduction of undefined.

@ArielGueta
Copy link

We can't use the required because it's not required. I'm not saying that this is the correct way things should have been done but Angular was "forgiven" in that matter and it's now difficult to migrate. I'm sure there are more applications that did the same.

@@ -51,6 +51,8 @@ export interface InputFunction {
<T>(): InputSignal<T | undefined>;
/** Declares an input of type `T` with an explicit initial value. */
<T>(initialValue: T, opts?: InputOptionsWithoutTransform<T>): InputSignal<T>;
/** Declares an input of type `T|undefined` without an initial value, but with input options */
<T>(initialValue: undefined, opts: InputOptionsWithoutTransform<T>): InputSignal<T | undefined>;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if that is not confusing. TS error messages are not super great, but I like the incentive to encourage the shorthand.

@devversion
Copy link
Member

We can't use the required because it's not required. I'm not saying that this is the correct way things should have been done but Angular was "forgiven" in that matter and it's now difficult to migrate. I'm sure there are more applications that did the same.

I guess it depends on whether your application is built using --strict / --strictPropertyInitialization. In the example above, the input may also be undefined. So it's actually unsafe/brittle already, as @JoostK pointed out.

If you were to convert from @Input to input(), and would not fix the type to include undefined, or use a required input— then you'd effectively choose to do this: input<string>(undefined as any)

@devversion
Copy link
Member

TGP green

@JoostK JoostK marked this pull request as ready for review September 5, 2024 17:24
@JoostK JoostK added the action: review The PR is still awaiting reviews from at least one requested reviewer label Sep 5, 2024
Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a comment

Choose a reason for hiding this comment

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

LGTM

Reviewed-for: public-api
Reviewed-for: fw-core

Copy link
Contributor

@thePunderWoman thePunderWoman 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

@pkozlowski-opensource pkozlowski-opensource added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 11, 2024
@thePunderWoman
Copy link
Contributor

This PR was merged into the repository by commit c93b510.

The changes were merged into the following branches: main

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

5 participants