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

Add typeguard for filtering out undefined #2502

Merged
merged 3 commits into from May 16, 2024

Conversation

Kevin-Chant
Copy link
Contributor

@Kevin-Chant Kevin-Chant commented May 9, 2024

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 like Array.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

const sortedFilteredGuardrails = sortAndFilterMetricsByTags(
guardrailDefs,
metricFilter
);

const retMetrics = sortedFilteredMetrics
.map((metricId) => getRow(metricId, false))
.filter((row) => row?.metric) as ExperimentTableRow[];
Copy link
Contributor Author

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

@@ -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;
Copy link
Contributor Author

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

Copy link

github-actions bot commented May 9, 2024

Your preview environment pr-2502-bttf has been deployed.

Preview environment endpoints are available at:

@Kevin-Chant Kevin-Chant requested a review from jdorn May 10, 2024 17:27
@Kevin-Chant Kevin-Chant force-pushed the kc/add-filter-defined-typeguard branch from 15d048a to 7b35dc9 Compare May 16, 2024 22:41
@Kevin-Chant Kevin-Chant merged commit c0a6afc into main May 16, 2024
3 checks passed
@Kevin-Chant Kevin-Chant deleted the kc/add-filter-defined-typeguard branch May 16, 2024 22:56
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.

None yet

2 participants