Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix getRelationsForEvent under TypeScript strict mode #9558

Merged
merged 31 commits into from
Nov 22, 2022

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Nov 8, 2022

Fix getRelationsForEvent under TypeScript strict mode

Spawning from an unrelated spot that I got yelled at for in #8354. There is too much to fix to include in that PR if we want to get that right:

Error: src/components/structures/TimelinePanel.tsx:1846:17 - Type '(eventId: string, relationType: RelationType, eventType: EventType | string) => Relations | undefined' is not assignable to type '(eventId: string, relationType: string, eventType: string) => Relations'.

Checklist

  • Tests written for new code (and old code if feasible)
  • Linter and other CI checks pass
  • Sign-off given on the changes (see CONTRIBUTING.md)

This change is marked as an internal change (Task), so will not be included in the changelog.

@MadLittleMods MadLittleMods added the T-Task Refactoring, enabling or disabling functionality, other engineering tasks label Nov 8, 2022
eventId: string,
relationType: RelationType | string,
eventType: EventType | string,
) => Relations | null | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not every event has relations so of course we should be able to return undefined.

Unless of course there can be an empty Relations object? Since Relations(relationType, eventType, client) requires relationType I don't think this is the case.

@MadLittleMods MadLittleMods changed the title Fix getRelationsForEvent tsc strictness Fix getRelationsForEvent under TypeScript strict mode Nov 8, 2022
Copy link
Contributor

@weeman1337 weeman1337 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Did a quick test on the Netlify Deployment 👍

src/components/views/messages/MPollBody.tsx Outdated Show resolved Hide resolved
src/components/views/messages/MPollBody.tsx Show resolved Hide resolved
src/components/views/messages/MPollBody.tsx Show resolved Hide resolved
"the top answer - assuming no best answer",
);
return "";
}
Copy link
Contributor

Choose a reason for hiding this comment

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

On the other hand: Having tests before fixing strict type things would give us confidence that it is still working (as expected).

@@ -484,7 +484,7 @@ export default class MPollBody extends React.Component<IBodyProps, IState> {
const totalVotes = this.totalVotes(votes);
const winCount = Math.max(...votes.values());
const userId = this.context.getUserId();
const myVote = userVotes?.get(userId)?.answers[0];
const myVote = userVotes?.get(userId!)?.answers[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can use ! here because a Map lookup with null or undefined will still give an undefined myVote which is acceptable in the logic below.

@turt2live turt2live merged commit 2393510 into develop Nov 22, 2022
@turt2live turt2live deleted the madlittlemods/strict-getRelationsForEvent branch November 22, 2022 03:54
@MadLittleMods
Copy link
Contributor Author

Thanks for the review @weeman1337 and merge @turt2live 🐾

amywalkerdev pushed a commit that referenced this pull request Nov 28, 2022
* Fix getRelationsForEvent tsc strictness

* Use shared type for GetRelationsForEvent

* Fix lint

* Add alternative type

* getRelationsForEvent is not required

* Relations are optional

* Reactions are optional

* We expect relations in these tests

* Add more protection if the eventID is not defined

* Allow null too

* Better test typing

* User ID is not necessary unless something is selected

* It's okay to [].includes(null)

* Null is as good as undefined here

* Null or undefined is good here

* We have some expectations for the tests

* The room and user can be undefined too

* Protec

* Reactions are optional

* Try match signatures

* Null or undefined

* More null or undefined

* Protec

* Fix typo (wrong variable)

* Remove optional params

See #9558 (comment)

* Fix up last maaaaybe relevant lint

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Task Refactoring, enabling or disabling functionality, other engineering tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants