-
Notifications
You must be signed in to change notification settings - Fork 154
fix(storage): types #818
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
Merged
+45
−45
Merged
fix(storage): types #818
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@solid-primitives/storage": minor | ||
| --- | ||
|
|
||
| types and resource usage |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: solidjs-community/solid-primitives
Length of output: 2020
🌐 Web query:
TypeScript infer type parameter from another type parameter constraint S extends Signal<T> inference from constraint only💡 Result:
TypeScript does not support direct inference of a type parameter (e.g., T) solely from the constraint of another type parameter (e.g., S extends Signal) when invoking a function [1]. In TypeScript, type parameters are primarily inferred from the actual types of the function's arguments, not from the constraints defined on the generic parameters themselves [1]. Because constraints are used as checks rather than as inference sites, defining a function like function example<T, S extends Signal>(signal: S) {... } often results in T failing to be inferred correctly or defaulting to unknown [1]. To work around this limitation, you can use the following common techniques: 1. Indexed Access Types: If the type T can be extracted from the structure of S, use indexed access types instead of a secondary type parameter. For example, if Signal has a property that holds the value, you can define the type as S['value'] or create a utility type to extract it [2][3]. 2. Conditional Types with
infer: Use a conditional type to extract the inner type T from the signal type S [4][5]. type GetSignalValue= S extends Signal? T: never; 3. Intersection Types at Usage: If you must rely on inference, you can sometimes improve it by using an intersection type in the function parameter to provide an additional "inference site" [1]. function example<S extends Signal>(signal: S & Signal) {... } 4. Avoid Dependent Generics: Where possible, refactor your code to remove the circularity or dependency between the generics, or accept that you may need to explicitly provide the type arguments when calling the function [6][1][3]. This behavior is a known limitation of the TypeScript compiler [1][7], and while various workarounds exist, no direct feature exists to infer generic constraints themselves as primary inference sources [1].Citations:
linkedSignalsignature angular/angular#60423🌐 Web query:
TypeScript infer from generic constraint S extends Signal<T> example compiler behavior💡 Result:
In TypeScript, the ability to infer a type from a generic constraint (such as S extends Signal) depends significantly on whether you are using the
inferkeyword within a conditional type or attempting to perform inference at the generic parameter level. ### Conditional Types andinferWhen you useinferinside a conditional type, it is specifically designed to extract parts of a type during type matching [1][2][3]. Since TypeScript 4.7, you can use theinfer T extends Constraintsyntax to combine inference and constraint checking in a single step [1]. Example:ts // Extract T from a Signal<T> type type GetSignalValue<S> = S extends Signal<infer T> ? T : never; // Using infer with an explicit constraint (TS 4.7+) type FirstString<T> = T extends [infer H extends string, ...any[]] ? H : never;This behavior is stable and well-supported [1]. ### Generic Constraints and Inference Limitations Attempting to infer type parameters directly from generic constraints (e.g.,<S extends Signal<T>>) in function signatures or type definitions often encounters limitations because TypeScript does not automatically "reverse" generic constraints to infer the underlying type parameters (likeT) [4][5]. 1. Inference Does Not Flow "Backward": When you define a generic type with a constraint like<S extends Signal<T>, T>, TypeScript often cannot automatically deduceTsimply from the presence ofSunlessTis explicitly included and constrained in the signature [5]. 2.unknownResults: In complex signatures—particularly those involving callbacks or signals—TypeScript may default tounknownfor inferred types because it cannot determine the specific type arguments without explicit help or simplified structural signatures [6][5]. 3. Compiler Workarounds: - Manual Specification: Forcing the user to specify both generics (e.g.,<S, T>(signal: S & Signal<T>)) is the most reliable way to avoidunknowninference [5]. -NoInferUtility: To control inference and prevent TypeScript from over-widening types, developers often use theNoInfer<T>utility type, which blocks inference for specific parts of a signature [6][7]. ### Summary of Compiler Behavior The compiler treatsinferas a structural matching tool within conditional types [1][2]. However, generic constraints (e.g.,S extends Signal<T>) are treated as bounds on existing types rather than sources for new inferences [4][5]. If you need to extractTfromS, you should use a conditional type mapping (e.g.,S extends Signal<infer T> ? T : never) rather than relying on the generic constraint definition itself to perform the extraction [1][2].Citations:
unknownif argument hasNoInfer<Generic>but returns the Generic microsoft/TypeScript#60922linkedSignalsignature angular/angular#60423Infer
Tfromsignal, not from theSconstraint.Thas no direct inference site here, sooptions.serialize/deserialize/synccan end up typed asunknowninstead of the signal element type. ExtractTfromSin the signature if you want those callbacks checked against the actual value type.🤖 Prompt for AI Agents