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
Add typeguard for filtering out undefined #2502
Conversation
const sortedFilteredGuardrails = sortAndFilterMetricsByTags( | ||
guardrailDefs, | ||
metricFilter | ||
); | ||
|
||
const retMetrics = sortedFilteredMetrics | ||
.map((metricId) => getRow(metricId, false)) | ||
.filter((row) => row?.metric) as ExperimentTableRow[]; |
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.
I checked the type definition for what row
could be (other than undefined) and it looks like row.metric
is always present, so the definition check is just on row
itself, hence isDefined
should be a drop-in replacement
packages/front-end/components/Settings/Team/SingleRoleSelector.tsx
Outdated
Show resolved
Hide resolved
@@ -62,8 +62,7 @@ function getExampleAttributes({ | |||
? ["foo", "bar"].map((v) => sha256(v, secureAttributeSalt)) | |||
: ["foo", "bar"]; | |||
} else if (datatype === "enum") { | |||
// @ts-expect-error TS(2532) If you come across this, please fix it!: Object is possibly 'undefined'. | |||
value = enumList.split(",").map((v) => v.trim())[0] ?? null; | |||
value = enumList?.split(",").map((v) => v.trim())[0] ?? null; |
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.
Tested in node -i
that this still behaves appropriately when enumList is either string
or undefined
:
> "foo"?.split(",").map(v => v.trim())[0] ?? null
'foo'
> undefined?.split(",").map(v => v.trim())[0] ?? null
null
Your preview environment pr-2502-bttf has been deployed. Preview environment endpoints are available at: |
15d048a
to
7b35dc9
Compare
Features and Changes
Found an easy fix for many instances of TS(2532) across the codebase while working on a separate bug. Added a new shared typeguard
isDefined
which will help methods likeArray.prototype.filter
to correctly infer the type of the result.Searched the codebase for both instances of 2532 and
filter(Boolean)
and updated the usage manually to reduce the likelihood of unintentional bugs. For cases where it wasn't obviously an easy fix, I left the original ts-expect-error and functionality in place.Dependencies
None?
Testing
There's too many files to do manual testing, so instead I'm focusing on thoroughly reading the PR & letting the test suite check for any large-scale issues