-
Notifications
You must be signed in to change notification settings - Fork 5k
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
refactor: convert icon-factory.js
to typescript
#23823
Conversation
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
icon-factory.js
to typescript
fa2d785
to
8e21b92
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #23823 +/- ##
===========================================
+ Coverage 67.36% 68.99% +1.63%
===========================================
Files 1282 1164 -118
Lines 50011 44513 -5498
Branches 12983 11887 -1096
===========================================
- Hits 33685 30709 -2976
+ Misses 16326 13804 -2522 ☔ View full report in Codecov by Sentry. |
Builds ready [8e21b92]
Page Load Metrics (1034 ± 508 ms)
Bundle size diffs
|
function iconExistsFor( | ||
address: string, | ||
tokenMetadata?: Partial<TokenMetadata>, | ||
): tokenMetadata is TokenMetadata { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be boolean here?
): tokenMetadata is TokenMetadata { | |
): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the iconExistsFor
function acts as a type guard via a type predicate (the tokenMetadata is TokenMetadata
part).
It enables the following, where tokenMetadata
is optional in iconExistsFor
, but not in imageElFor
. TypeScript isn't "smart" enough to infer that tokenMetadata
is never null
/undefined
when iconExistsFor
returns true
, so a type predicate is used to give TypeScript that information, otherwise we'd have to cast TokenMetadata
or check for null
/undefined
again inside the if
statement.
if (iconExistsFor(address, tokenMetadata)) {
return imageElFor(tokenMetadata);
}
See https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates for details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks ! it makes sense !
8e21b92
to
08b602a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good to me, and I tested them locally.
Quality Gate passedIssues Measures |
Builds ready [f4bfed4]
Page Load Metrics (457 ± 372 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
No description provided.