Skip to content

fix(compiler): properly type forwardRef and resolveForwardRef - #11813

Closed
NickClark wants to merge 1 commit into
angular:masterfrom
k3integrations:forward-ref-11638
Closed

NickClark wants to merge 1 commit into
angular:masterfrom
k3integrations:forward-ref-11638

Conversation

@NickClark

Copy link
Copy Markdown

Please check if the PR fulfills these requirements

What kind of change does this PR introduce? (check one with "x")

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior? (You can also link to an open issue here)
#11638

What is the new behavior?

Does this PR introduce a breaking change? (check one with "x")

[ ] Yes
[x] No

If this PR contains a breaking change, please describe the impact and migration path for existing applications: ...

Other information:

Makes forwardRef and resolveForwardRef return more meaningful types. Now when resolving a forward reference, TypeScript knows what type the returned reference is.

Closes #11638

@NickClark
NickClark force-pushed the forward-ref-11638 branch 2 times, most recently from e8b5367 to e8048d5 Compare September 22, 2016 00:36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

const

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

q: Is the cast really necessary here ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I thought it was. I was getting the error: SELECT_MULTIPLE_VALUE_ACCESSOR' has or is using name 'ForwardRefFn' from external module "/Users/nick/Code/k3/angular/modules/@angular/core/src/di/forward_ref" but cannot be named.. But just now, going back I realize now all I needed was to import the forwardRefFn. 😶

@vicb vicb added the action: review The PR is still awaiting reviews from at least one requested reviewer label Sep 22, 2016

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Q: I don't see how this type relates to ForwardRef, what about renaming this to Factory<T> ?

WDYT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need this type at all ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No. I just get carried away with naming my types. I was just trying to hint at it's purposed, since it's really just a function that we decorate, and wanted to clearly differentiate it from the type that is returned.

Shall I just change the forwardRef to this: function forwardRef<T>(forwardRefFnSrc: () => T): ForwardRefFn<T>? That definitely seems clear enough 😄

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@vicb

vicb commented Sep 22, 2016

Copy link
Copy Markdown
Contributor

One minor question, otherwise LGTM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

return _isForwardRefFn(type) ? type() : type;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ternary: ✅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need && t.hasOwnProperty('__forward_ref__') here ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It does seem redundant. I actually just kept the original check here, so I'm not sure if the original author had some reason for it. Is there some reason we need to ensure that the property is not an inherited one?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I guess if there's no test for it...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe leave it like this for now... thanks

@vicb vicb added pr_state: LGTM action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Sep 22, 2016
@NickClark

Copy link
Copy Markdown
Author

Should be good to go. :shipit:

@vicb vicb Sep 24, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would this work ?

export function forwardRef<T>(typeFactory: () => T): ForwardRefFn<T> {
 typeFactory['__forward_ref__'] = forwardRef;
 typeFactory['toString'] = function() { return stringify(this()); };
 return typeFactory;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

make __forward_ref__ a const

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll look at this when I get back to my dev computer. Do we allow implicit any?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Also, if we use the latest TypeScript I think it's supposed to allow something like this now: forwardRefFn = <ForwardRefFn<T>>forwardRefFn

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm changing the name to typeFactory. Not sure I'm clear on what else you're asking for here. Are we accessing object attributes via strings to help prevent minification? Seems like that wouldn't happen.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this formatted ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

oh forget about it... that's ok with this clang-format version

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ya, the tooling didn't catch it, so I assumed we were good here. 😄

@vicb

vicb commented Sep 24, 2016

Copy link
Copy Markdown
Contributor

one minor nit

@vicb vicb Sep 24, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/cc @mhevery how well do this play with minification ? (ie 'forward_ref') is hardcoded.

Should it be return isFunction(t) && t.__forward_ref__ as ForwardRef === forwardRef;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove t.hasOwnProperty('__forward_ref__') (which would not play well with minification) & rebase

@vsavkin vsavkin added the area: core Issues related to the framework runtime label Oct 4, 2016
@vicb

vicb commented Oct 10, 2016

Copy link
Copy Markdown
Contributor

could you please rebase and integrate required changes ?

@NickClark

Copy link
Copy Markdown
Author

@vicb Ya, sorry about the delay, was gone on vacation.

@vicb

vicb commented Oct 11, 2016

Copy link
Copy Markdown
Contributor

No pb Nick, only have time to get back to this PR now

@NickClark

Copy link
Copy Markdown
Author

Had some questions in regard to the requested refactor of forwardRef

@vicb

vicb commented Oct 13, 2016

Copy link
Copy Markdown
Contributor

Yep, what ?

@NickClark

Copy link
Copy Markdown
Author

@vicb ☝️ #11813 (comment)

@mhevery

mhevery commented Oct 20, 2016

Copy link
Copy Markdown
Contributor

@vicb ping

@NickClark
NickClark force-pushed the forward-ref-11638 branch 3 times, most recently from 82f1839 to 77ec25d Compare October 24, 2016 19:00
@NickClark

Copy link
Copy Markdown
Author

@vicb Hopefully that'll do 😃 :shipit:

@tbosch

tbosch commented Jun 6, 2017

Copy link
Copy Markdown
Contributor

Hi,
as this is a breaking change we are willing to take in the 5.0 release. Could you rebase against master if you are still interested in landing this PR?

@tbosch tbosch removed deprecated label: LGTM (DO NOT USE ANY MORE) action: review The PR is still awaiting reviews from at least one requested reviewer action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jun 6, 2017
@NickClark

Copy link
Copy Markdown
Author

Ya, I'll try to get to this in the next day or so.

@NickClark
NickClark force-pushed the forward-ref-11638 branch from 77ec25d to ee12e03 Compare June 8, 2017 22:28
Makes `forwardRef` and `resolveForwardRef` return more meaningful  types. Now when resolving a forward reference, TypeScript knows what type the returned reference is.

Closes angular#11638
@NickClark
NickClark force-pushed the forward-ref-11638 branch from ee12e03 to 64b2609 Compare June 9, 2017 01:15
@NickClark

Copy link
Copy Markdown
Author

I believe it's good to go.

@NickClark

Copy link
Copy Markdown
Author

@tbosch Is anything else needed?

@tbosch

tbosch commented Jun 27, 2017

Copy link
Copy Markdown
Contributor

We need to create a branch for v5 before we can land this...

@vicb vicb added this to the v6-candidates milestone Feb 13, 2018
@splincode

splincode commented Feb 28, 2018 •

Copy link
Copy Markdown
Contributor

@NickClark @mhevery @vicb What is the status of the task?

@alexeagle alexeagle removed this from the v6-candidates milestone Aug 10, 2018
@alexeagle

Copy link
Copy Markdown
Contributor

Closing pull requests from 2016, sorry this got stale and was never merged. Please open a new one if you still care about this change.

@alexeagle alexeagle closed this Sep 18, 2018
@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better Typing for forwardRef and resolveForwardRef

9 participants