support "star projections" in generics
See original GitHub issueSuggestion
🔍 Search Terms
star projection
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
in kotlin, you can omit a generic from a type annotation when you don’t care what its value is:
class Foo<T: Number>
fun foo(value: Foo<*>) {}
more info: https://typealias.com/guides/star-projections-and-how-they-work/
in typescript, to do the same thing you need to use any
, which is gross:
type Foo<T extends number> = T
declare function foo(value: Foo<any>): void
or simply duplicate the bound, which isn’t convenient:
type Foo<T extends number | string | boolean> = T
declare function foo(value: Foo<number | string | boolean>): void
📃 Motivating Example
it’s especially useful for types that have multiple bounded generics
type ThingWithLotsOfGenerics<Type1 extends Base1, Type2 extends Base2, Type3 extends Base3> = {}
declare function foo(value: ThingWithLotsOfGenerics<*, *, *>): void
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Star-Projections and How They Work - Dave Leeds on Kotlin
We've discovered three ways to accept all kinds of a generic: ... Star-projection is the favored approach, because it's idiomatic Kotlin. It's ...
Read more >How to tell Kotlin that star projections represent the same type ...
Then, we know by some internal logic that this provider always returns A's and B's with the same type parameter under the star-projections....
Read more >Generics In Kotlin (Part 3) - Medium
In this post I want to go over more concepts around Generics in Kotlin ... Kotlin's star projection is similar to unbounded wildcard...
Read more >Understanding star projections of bound generic type - Support
I have a class declared as Query<out T : Any>. Then I have this method: fun bindFoo(liveData: LiveData<Query<*>>) {.
Read more >Support specifying polymorphic serializer for star-projected ...
What is your use-case and why do you need this feature? I want to serialize a type which contains star-projected properties: Survey below....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@MartinJohns if you use
any
:if you use
unknown
:You can’t use
unknown
because it’s wider than the bound. and you shouldn’t useany
because it then allows things from outside the bound.