Skip to content

fix: Replace raw in operator checks with keyof-validated type guards#4306

Merged
Who-is-PS merged 7 commits intomainfrom
dev-v3-philosr-refactor-3
Mar 9, 2026
Merged

fix: Replace raw in operator checks with keyof-validated type guards#4306
Who-is-PS merged 7 commits intomainfrom
dev-v3-philosr-refactor-3

Conversation

@Who-is-PS
Copy link
Copy Markdown
Contributor

@Who-is-PS Who-is-PS commented Feb 26, 2026

Description

Replaces raw 'prop' in obj checks with keyof-validated alternatives across 6 components to prevent a class of bugs where TypeScript silently accepts invalid property name strings in in expressions.

This is a COE action item AWSUI-59006 from the property filter bug (YpPBAPz2BrqF) where a type change during refactoring silently broke a 'propertyKey' in newToken check because TypeScript doesn't validate the in operator — 'banana' in obj compiles without error.

Changes:

  • Added isStackableItem() and isRefObject() type guards to flashbar/utils.ts
  • Updated collapsible-flashbar.tsx to use isStackableItem() (2 checks) and isRefObject() (1 check)
  • Updated non-collapsible-flashbar.tsx to use isRefObject() (1 check)
  • Updated mixed-line-bar-chart/utils.ts to use keyof in isYThreshold() and isXThreshold() (2 checks)
  • Updated side-navigation/util.tsx to use inline keyof variables for 'href' and 'items' checks (2 checks)
  • Updated top-navigation/parts/utility.tsx to use inline keyof for 'items' check (1 check)
  • Updated button-dropdown/internal.tsx to use inline keyof for 'items' and 'badge' checks (2 checks)
  • Updated button-group/item-element.tsx to use inline keyof for 'popoverFeedback' check (1 check)

12 checks across 7 files, 6 components. No behavioral changes. Pure refactor for type safety.

Related links, issue #, if available: AWSUI-59006 and Tech Proposal. (k9wvA3eNMiZ9).

How has this been tested?

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.43%. Comparing base (b8108b4) to head (c98b616).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4306   +/-   ##
=======================================
  Coverage   97.43%   97.43%           
=======================================
  Files         897      897           
  Lines       26350    26358    +8     
  Branches     9516     9516           
=======================================
+ Hits        25675    25683    +8     
+ Misses        669      632   -37     
- Partials        6       43   +37     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Who-is-PS Who-is-PS marked this pull request as ready for review March 3, 2026 16:22
@Who-is-PS Who-is-PS requested a review from a team as a code owner March 3, 2026 16:22
@Who-is-PS Who-is-PS requested review from amanabiy and georgylobko and removed request for a team March 3, 2026 16:22
Copy link
Copy Markdown
Member

@georgylobko georgylobko left a comment

Choose a reason for hiding this comment

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

In overall I'm not following what is fundamentally different in this function after refactoring. Could you add a u tests that fails before the refactoring and passes after?

type SearchableTagFields = 'tags' | 'filteringTags';

const isGroup = (option: AutosuggestItem) => 'type' in option && option.type === 'parent';
const isGroup = (option: AutosuggestItem) => option.type === 'parent';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the type is optional

Suggested change
const isGroup = (option: AutosuggestItem) => option.type === 'parent';
const isGroup = (option: AutosuggestItem) => option?.type === 'parent';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated with optional chaining

const useEntered = 'type' in option && option.type === 'use-entered';
const isParent = 'type' in option && option.type === 'parent';
const isChild = 'type' in option && option.type === 'child';
const useEntered = option.type === 'use-entered';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const useEntered = option.type === 'use-entered';
const useEntered = option?.type === 'use-entered';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And the same for isParent and isChild

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, applied ?. to all three.

return key in item;
}

export function isRefObject<T>(ref: React.Ref<T>): ref is React.RefObject<T> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should the ref argument be typed as any? When you call this function, you don't know the type of ref yet - that's exactly what this function is supposed to check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to unknown instead of any since we already do !== null && typeof ref === 'object' runtime checks inside.

export const isGroup = (option?: OptionDefinition | OptionGroup): option is OptionGroup =>
!!option && 'options' in option && !!option.options;
export const isGroup = (option?: OptionDefinition | OptionGroup): option is OptionGroup => {
const key: keyof OptionGroup = 'options';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not following what is fundamentally different in this function after refactoring. Could you add a u test that fails before the refactoring and passes after?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The runtime behavior is identical — no unit test can distinguish before/after because both produce the same output for every input.

The difference is at compile time. If someone renames options to items on OptionGroup:

  • Before: 'options' in option — compiles fine, silently returns false for all groups (the exact COE bug pattern)
  • After: const key: keyof OptionGroup = 'options'fails to compile: Type '"options"' is not assignable to type '"items" | ...'

@Who-is-PS Who-is-PS force-pushed the dev-v3-philosr-refactor-3 branch from a4a78de to 41ef22e Compare March 6, 2026 11:00
@Who-is-PS Who-is-PS added this pull request to the merge queue Mar 9, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 9, 2026
@Who-is-PS Who-is-PS added this pull request to the merge queue Mar 9, 2026
Merged via the queue into main with commit e3c99fb Mar 9, 2026
50 checks passed
@Who-is-PS Who-is-PS deleted the dev-v3-philosr-refactor-3 branch March 9, 2026 09:41
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.

2 participants