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.

Suggestion: Upper-bound generic type constraints

See original GitHub issue

This is a proposal for generic structural supertype constraints.

I’ll first detail the proposal, and then get into a discussion about use-cases.

Proposal

A supertype constraint is a constraint on a generic type parameter to be the structural supertype of another type. It’s like a subtype/inheritance constraint, but from the other direction. For example, let’s say we have a square:

interface Square {width : number; height : number}

In this suggested syntax, you could write:

performAction<T extended by Square>(partialSquare : T) {}

This means the type T must be a structural supertype of square. So the potential candidates for T are:

{width : number}, {height : number}, Square, {}

It’s an established kind of type constraint, not something I just made up. Although it’s not exactly common, some languages do implement this feature, such as Scala. In Scala, you can write:

def performAction[T >: Square](obj : T) = { ... }

To express a supertype/upper bound constraint. In this case, of course, the constraint isn’t structural – T must declare that it implements Square.

Utility

In most languages, including Scala, this kind of constraint isn’t very useful. It only comes up in certain specific situations.

However, Javascript libraries often have this kind of API, where you’re allowed to specify the partial properties of an object to modify it (the rest remain at their previous value).

In many cases, you can support this by having optional interface members, but isn’t always possible or correct.

An important example is React, which defines a method called setState:

class Component {
    var state;

    setState(partialState) {
        //merges the properties of partialState with the current state.
    }
}

The right signature for this method should be:

class Component<Props, State> {
    setState<PartialState extended by State>(partialState : PartialState) {
        //merges the properties of partialState with the current state.
    }
}

Currently,the definition files state that it is:

setState(fullState : State) {
    //merges the properties of partialState with the current state.
}

Which doesn’t fully capture the functionality of the method.

Notes

The suggested syntax doesn’t give us a nice way of combining both subtype and supertype constraints. One possibility is:

exampleMethod<T extends LowerBound and extended by UpperBound>

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:25
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
timsuchanekcommented, Mar 14, 2019

@GregRos this is an interesting idea and I also think the language would benefit from such a keyword.

As a workaround you can set an upper boundary in the param using a helper type:

type Full = {
  a: string
  b?: string
}

/**
 * Subset
 * @desc From `T` pick properties that exist in `U`
 */
type Subset<T, U> = { [key in keyof T]: key extends keyof U ? T[key] : never }

function acceptFullAndMore<T extends Full>(param: T): T {
  return param
}

acceptFullAndMore({a: '', c: 1}) // Compiler allows `c`, even if it's not part of `Full`

function acceptFull<T extends Full>(param: Subset<T, Full>): T {
  return param
}

acceptFull({a: '', c: 1}) // Compiler throws here 🙏
2reactions
mhegazycommented, Oct 10, 2016
Read more comments on GitHub >

github_iconTop Results From Across the Web

generics - Is it possible to specify both upper and lower bound ...
I don't believe so - as far as I can tell from the language specification, "super" is only valid for wildcard types in...
Read more >
How to choose Upper & Lower bounds in Java Generics?
This Blog explains the upper/lower/no bound of Java Generics, with the guidelines on how to choose the bounds on Java Generics.
Read more >
When (and when not) to Use Type Parameter Constraints in ...
Kotlin allows you to constrain a generic's type parameter to an upper bound class, limiting the range of types that it can accept....
Read more >
Bounded Type Parameters - The Java™ Tutorials
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in...
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, similar like ......
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