Skip to content

Conversation

@blazejkustra
Copy link
Contributor

@blazejkustra blazejkustra commented Nov 18, 2025

Details

After fixing type safety of OnyxUpdate we observed a massive TS slowdown due to complexity of this type, in order to improve it we decided to make it a generic so it doesn't produce huge union types of objects that are making TS and ESlint slow.

Relevant discussion: https://expensify.slack.com/archives/C01GTK53T8Q/p1762617983543509

Related Issues

Expensify/App#73830

Automated Tests

N/A

Manual Tests

N/A (purely type change)

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js N/.Aor at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

N/A

@blazejkustra blazejkustra marked this pull request as ready for review November 18, 2025 07:52
@blazejkustra blazejkustra requested a review from a team as a code owner November 18, 2025 07:52
@blazejkustra
Copy link
Contributor Author

@fabioh8010 @roryabraham All yours 😄

@melvin-bot melvin-bot bot requested review from rlinoz and removed request for a team November 18, 2025 07:52
@mountiny mountiny requested a review from roryabraham November 18, 2025 09:28
@blazejkustra
Copy link
Contributor Author

After testing in the repo I noticed few edge cases, let's hold with reviews for now.

@blazejkustra blazejkustra marked this pull request as draft November 18, 2025 13:28
@blazejkustra blazejkustra marked this pull request as ready for review November 18, 2025 13:50
@fabioh8010
Copy link
Contributor

After testing in the repo I noticed few edge cases, let's hold with reviews for now.

Is it ready to review now @blazejkustra ?

@blazejkustra
Copy link
Contributor Author

Yes @fabioh8010 🙌

roryabraham
roryabraham previously approved these changes Nov 18, 2025
Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

@blazejkustra Looks like Onyx.update() is not being able to recognise Onyx.METHOD.SET_COLLECTION and Onyx.METHOD.MERGE_COLLECTION methods.

Screenshot 2025-11-19 at 07 59 40

@blazejkustra
Copy link
Contributor Author

blazejkustra commented Nov 19, 2025

@blazejkustra Looks like Onyx.update() is not being able to recognise Onyx.METHOD.SET_COLLECTION and Onyx.METHOD.MERGE_COLLECTION methods.

Screenshot 2025-11-19 at 07 59 40

FYI I'm still working on this, basically Onyx.update doesn't narrow the type correctly. Why is this so hard to type 😭

@roryabraham roryabraham changed the title Refactor OnyxUpdate type to support generics for improved performance [WIP] Refactor OnyxUpdate type to support generics for improved performance Nov 19, 2025
@roryabraham
Copy link
Contributor

roryabraham commented Nov 19, 2025

NAB suggestion: Implement some type-tests in the manner of type-fest.

We had a type regression on this code once before...

@blazejkustra
Copy link
Contributor Author

NAB suggestion: Implement some type-tests in the manner of type-fest.

We had a type regression on this code once before...

I introduced type tests as part of this PR, I have nothing against adding more to cover more cases :)

@blazejkustra blazejkustra marked this pull request as draft November 19, 2025 21:17
@blazejkustra
Copy link
Contributor Author

NoInfer utility type is a life saver! 🤯

I initially introduced ExpandOnyxKeys so specifying the generics is easier:

const onyxUpdateCollectionMember: OnyxUpdate<typeof ONYX_KEYS.COLLECTION.TEST_KEY> = {
    onyxMethod: 'set',
    key: `${ONYX_KEYS.COLLECTION.TEST_KEY}1`,
    value: {
        str: 'test1',
    },
};

instead of this complex type:

const onyxUpdateCollectionMember: OnyxUpdate<`${typeof ONYX_KEYS.COLLECTION.TEST_KEY}${string}`> = {
    ...
};

I realized that narrowing completely broke because of this type. Onyx.update() couldn’t infer from the literal key, because the ${TKey}${string} part was “too wide” and TS forfeited 😄

NoInfer prevented TypeScript from using the ${K}${string} branch to infer the generic TKey, so inference falls back to the actual key that is passed 🎉 It's first time I'm using this type and I'm amazed!

@blazejkustra
Copy link
Contributor Author

I made this PR draft again since I'm OOO 20-26 Nov.

The PR is ready to review but I don't think merging is a good idea as it could block other updates in onyx while I'm gone.

Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

Nice, and Intelissense looks faster now 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants