fix(node): tighten StateSelector to typed behavior keys#3683
Merged
Conversation
Resolves the TODO from 2d9d35f. The previous comment claimed the SupportedBehaviors index signature blocked tightening, but concrete EndpointType.For<T> already preserves the literal MapOf<> shape with narrow keys. Use a `string extends K` discriminator inside StateSelector so concrete typed endpoints get BehaviorSelection<BehaviorAt<T,K>> (typed attribute keys, autocomplete, rejection of unknown keys), and unbound `T extends EndpointType` falls back to RawBehaviorSelection. Adds a @ts-expect-error type-level test asserting unknown attribute keys are rejected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens the @matter/node endpoint type helpers so StateSelector<T> can use concrete behavior state keys when the caller has a fully typed EndpointType.For<T>, while still falling back to raw string selections for widened generic endpoint types. It improves the developer experience around Endpoint.get() typing without changing the runtime behavior of endpoint reads.
Changes:
- Refines
StateSelector<T>to selectBehaviorSelectionfor literal behavior maps and keepRawBehaviorSelectionfor generic/index-signature behavior maps. - Removes the old TODO in
Endpoint.tsby implementing the type-level narrowing directly. - Adds a compile-time
@ts-expect-errorassertion to verify unknown attribute keys are rejected for a concretely typed endpoint.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
packages/node/src/endpoint/Endpoint.ts |
Updates the StateSelector<T> type to distinguish literal behavior keys from widened generic behavior maps. |
packages/node/test/endpoint/EndpointGetTypesTest.ts |
Adds a type-level regression test for rejecting invalid attribute keys on typed endpoint selections. |
Adds a type-level equality assertion that the `string extends K` fallback branch of `StateSelector<T>` resolves to `RawBehaviorSelection` for unbound `T extends EndpointType`, plus a runtime-shape assertion that arbitrary string-keyed attribute lists remain assignable. Closes the regression gap noted by Copilot review on PR #3683 — without this, a refactor that drops or reorders the fallback branch could silently break generic callers that relied on raw string selections. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… path Adds a type-level regression test that builds an endpoint via the actual `EndpointType(...)` + `SupportedBehaviors(...)` + `MapOf<>` pipeline and asserts: (a) `keyof behaviors` stays narrow to the literal id rather than widening to `string`, and (b) `StateSelector<T>` accepts known State keys and rejects unknown ones. Closes the gap noted by Copilot review on PR #3683 — the previous tests only used a hand-built intersection (`EndpointType & { behaviors: { fake: ... } }`) that would still pass even if `EndpointType.For<T>` or `MapOf<T>` regressed for real, factory- constructed endpoints. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
StateSelector<T>now produces typed attribute keys per behavior for concreteEndpointType.For<T>callers (autocomplete + rejection of unknown attribute names) while keepingRawBehaviorSelectionas the fallback for unbound genericT extends EndpointType.string extends Kinside the mapped type detects the widened-key case (index-sig path) vs the narrow literal-keyed case fromMapOf<>. No changes toSupportedBehaviorsitself.@ts-expect-errortype-level test asserting unknown attribute keys are rejected.The earlier TODO claimed the
SupportedBehaviors = Record<string, Behavior.Type>index signature blocked the tightening; investigation showed concreteEndpointType.For<T>["behaviors"]is already the literalMapOf<>shape (no index sig), sokeyofis narrow there. The conditional handles both shapes.🤖 Generated with Claude Code