Intellisense: support filtering out options with invalid this
param constraints
#49510
Closed
5 tasks done
Labels
Declined
The issue was declined as something which matches the TypeScript vision
Suggestion
An idea for TypeScript
Suggestion
this
-param constraints are super useful for providing extensions to generic structures under certain conditions informed by type parameters.Playground
In C# and Kotlin this kind of thing can be accomplished with extension functions. In Rust, Scala and Haskell, there are traits, implicit conversions and typeclasses. I understand that Typescript itself can't support extension functions in this way for a few reasons, so I understand that there's a cap on flexibility here, but by having an option for Intellisense to filter out functions not compatible with the bound
this
parameter, things would at least be improved for library authors, as highly general libraries could improve their ergonomics without generating an unusably massive search space for users.🔍 Search Terms
class generic type this parameter constraint intellisense tsserver
✅ Viability Checklist
My suggestion meets these guidelines:
📃 Motivating Example
I'm working on a fairly abstract library for representing data dependencies as a lazily evaluated graph. Where graph nodes contain functions, I'd like to be able to offer
invoke
extensions to work on the inner function easily. For inner arrays, I'd like to do the same kind of thing, and so on. Monad transformer type stuff, but in a more familiar style to most developers.One option here today is just to have top-level classes like
FunctionNode
andArrayNode
, and permit those names to leak into userland. That's not entirely unacceptable, but it's not ideal, and Typescript has this fantasticthis
-param constraint feature which is perfect. Except that if I want to offer a meaningful volume of these functions, they pollute the IntelliSense search space for users at all times, not just when they apply.💻 Use Cases
I see this change being helpful to authors of similar libraries, especially in the reactive programming domain.
The text was updated successfully, but these errors were encountered: