Skip to content

Narrow return type of Performance/PerformanceObserverEntryList.getEntriesByType by entry type literal - #64338

Draft
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-performace-getentriesbytype-return-type
Draft

Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-performace-getentriesbytype-return-type

Conversation

Copilot AI commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

getEntriesByType was typed to always return PerformanceEntryList (PerformanceEntry[]), even though the runtime result is always a specific PerformanceEntry subclass determined by the type argument. This forced casts or caused false TS2339 errors, e.g. performance.getEntriesByType('navigation').some(e => e.type === 'reload') failed because type doesn't exist on PerformanceEntry.

Changes

  • lib.dom.d.ts: Added literal-string overloads to getEntriesByType on both Performance and PerformanceObserverEntryList, mapping each known entry type to its concrete subclass, with the existing string overload retained as a fallback for unknown types:
    • "navigation"PerformanceNavigationTiming[]
    • "resource"PerformanceResourceTiming[]
    • "mark"PerformanceMark[]
    • "measure"PerformanceMeasure[]
    • "paint"PerformancePaintTiming[]
    • "event"PerformanceEventTiming[]
    • "largest-contentful-paint"LargestContentfulPaint[]
  • lib.webworker.d.ts: Added the matching overload subset ("mark", "measure", "resource") since only those subclasses exist in the worker global scope.
  • Test: Added performanceGetEntriesByType.ts compiler test covering each overload and the generic fallback case.
function pageWasReloaded(): boolean {
    return performance
        .getEntriesByType('navigation') // now PerformanceNavigationTiming[]
        .some(entry => entry.type === 'reload'); // no longer an error
}

…ByType

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 19, 2026
Copilot AI changed the title [WIP] Fix return type for performance.getEntriesByType in lib.dom.d.ts Narrow return type of Performance/PerformanceObserverEntryList.getEntriesByType by entry type literal Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS 5.4.5: Return type for performance.getEntriesByType is inaccurate

2 participants