Skip to content
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

FED-2138 Fix createContext typing #875

Merged
merged 6 commits into from
Jan 30, 2024
Merged

Conversation

greglittlefield-wf
Copy link
Contributor

Motivation

The current typing for createContext is incorrect/unsound.

Context<TValue> createContext<TValue>([TValue? defaultValue, int Function(TValue?, TValue?)? calculateChangedBits]) {...}

But if you don't provide a default value, the value will be null, which isn't a TValue when TValue is a non-nullable type. And also, it's possible to provide null even when TValue is non-nullable.

This typing problem is essentially the same as useRef, for which we had to create useRefInit (link), and deprecated the arg to useRef. That way, we won't have to break createContext, and people passing the deprecated argument will get a deprecation hint telling them to move to the new one.

Changes

  • Update createContext to always return contexts with nullable type, since we can't guarantee that a default value was provided
  • Add createContext, which allows creation of contexts with non-nullable types, and required specifying a default value
  • Doc comments
    • Update examples to default values, include a useContext example
    • Update links to point to new React docs
  • Update tests, add test coverage for default values

Release Notes

Review

See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.

Please review:

QA Checklist

  • Tests were updated and provide good coverage of the changeset and other affected code
  • Manual testing was performed if needed

Merge Checklist

While we perform many automated checks before auto-merging, some manual checks are needed:

  • A Frontend Frameworks Design member has reviewed these changes
  • There are no unaddressed comments - this check can be automated if reviewers use the "Request Changes" feature
  • For release PRs - Version metadata in Rosie comment is correct

@aviary3-wk
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

@rmconsole5-wk rmconsole5-wk changed the title Fix createContext typing FED-2138 Fix createContext typing Jan 24, 2024
@greglittlefield-wf greglittlefield-wf marked this pull request as ready for review January 24, 2024 23:41
Copy link
Contributor

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

Just a typo and some confusing doc comments... otherwise I'm plus one @greglittlefield-wf

test('createContextInit() returns a correctly typed object', () {
final context = createContextInit<int>(0);
expect(context, isA<Context>());
expect(context, isA<Context<int>>(), reason: 'should have a non-nullable generic type');
Copy link
Contributor

Choose a reason for hiding this comment

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

NICE

@greglittlefield-wf
Copy link
Contributor Author

@aaronlademann-wf Good catches on the doc comments, addressed in 2bba27d!

Copy link
Contributor

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

+10

  • Passing CI

@greglittlefield-wf greglittlefield-wf merged commit 7d88c8c into v5_wip Jan 30, 2024
9 checks passed
@greglittlefield-wf greglittlefield-wf deleted the fix-context-typing branch January 30, 2024 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants