Repository navigation
fix(types): stop LabelValues distributing over label-name unions - #869
Open
sarankumar1325 wants to merge 1 commit into
Open
sarankumar1325 wants to merge 1 commit into
sarankumar1325 wants to merge 1 commit into
Conversation
LabelValues<T> used a naked type parameter in its conditional, so for labelNames ['a', 'b'] it became a union of single-key objects and reading labels back from get() failed to compile (values[0].labels.a). Wrap both sides in a tuple so the label names stay one object type. Fixes prometheus#861 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #861
LabelValues<T>usesTas a naked type parameter in its conditional, so TypeScript distributes it over a union of label names. ForlabelNames: ['a', 'b']the type becomesPartial<Record<'a', ...>> | Partial<Record<'b', ...>>. Writes still compile, but reading labels back does not:This wraps both sides of the conditional in a tuple (
[T] extends [NoLabelNameType]), so the label names stay one object type with every key. The no-label case (T = never) now resolves toPartial<Record<string, never>>as intended, instead ofnever.Tests:
test/typescript.tsthat readslabels.a/labels.bfromget()on a two-label counter, plus a@ts-expect-errorfor an undeclared label.tsc --project .fails on main with TS2339 and passes with this change.npm run lint,npm run check-prettierandjest(651 tests) pass.