question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support "star projections" in generics

See original GitHub issue

Suggestion

🔍 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:open
  • Created 2 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DetachHeadcommented, Oct 26, 2021

@MartinJohns if you use any:

type Foo<T extends number> = T

declare function foo(value: Foo<any>): void

foo(1)
foo({a: "asdf"}) //no error???

if you use unknown:

//error: Type 'unknown' does not satisfy the constraint 'number'.
declare function foo(value: Foo<unknown>): void
1reaction
KotlinIslandcommented, Oct 26, 2021

You can’t use unknown because it’s wider than the bound. and you shouldn’t use any because it then allows things from outside the bound.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found