镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Suggestion: Upper-bound generic type constraints #9252

Description

@GregRos

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>

Activity

  1. Strate commented on Jun 19, 2016

    @Strate

    Possible duplicate of
    #4889
    #371

  2. GregRos commented on Jun 20, 2016

    @GregRos
    Author

    Artur Eshenbrener (@Strate), Those really aren't duplicates at all. They're very different, both conceptually and practically.

    That said, I'm quite surprised this hasn't been proposed previously, so I suspected it was a duplicate, although I couldn't find anything.

  3. Strate commented on Jun 20, 2016

    @Strate

    Greg Ros (@GregRos) that tickets (especially #4889) contains discussion very close to your suggestion. For example, see this comment, or that. I wouldn't say that is it very different.

  4. GregRos commented on Jun 20, 2016

    @GregRos
    Author

    Open Source Projects from State (@State) yup, those comments are exactly my proposal, though the proposals themselves are different. I'm not sure if that means this is a duplicate or not.

    Incidentally, I had no idea Java supported this kind of constraint. I thought it was just a Scala thing.

    On second thought, #4889 can be used to get the same effect in some cases. One example where it can't is the following:

    blah<Partial extended by Full>(partial : Partial) : Partial {
        return partial;
    } //type information is preserved.
    

    And one example where that proposal can do something that mine can't:

    blah() : partial Full { //Full : {a : number, b : number}
        return {}; //or:
        return {a : 4}; //etc
    }
    

    e.g. The function can return different supertypes of Full. This partial type is basically the same as a supertype constraint combined with existential types. It lets you say "X is some supertype of Foo, but no one knows which one".

    #371 is similar to #4889. So you're right, on second thought, they're not very different.

  5. mhegazy commented on Oct 10, 2016

    @mhegazy
    Contributor

    this should be covered by #11233 (and #4889).

  6. locked and limited conversation to collaborators on Jun 19, 2018
  7. added
    SuggestionAn idea for TypeScript
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    and removed
    DuplicateAn existing issue was already created
    on Oct 9, 2018
  8. unlocked this conversation on Oct 9, 2018
  9. timsuchanek commented on Mar 14, 2019

    @timsuchanek
    Contributor

    Greg Ros (@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 🙏
  10. laurensdijkstra commented on May 11, 2019

    @laurensdijkstra

    this should be covered by #11233 (and #4889).

    How could the previous example from Greg Ros (@GregRos) be achieved with the Partial<> type?

    blah<Partial extended by Full>(partial : Partial) : Partial {
        return partial;
    } //type information is preserved.
    

    (where Partial is not actually the new Partial type from Typescript but his own type)

  11. truemogician commented on Nov 6, 2022

    @truemogician

    Greg Ros (@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 🙏

    Such a workaround requires a specific upper bound. However, in most cases, the upper bound is quite universal, like string, object, or simply any. We only want to filter out certain types without an exact supertype.
    These scenarios seem to fit in the semantics of Exclude<T, U>. However, this utility type also requires the supertype T to be specific, namely, union types. Universal types like string don't work as well, for instance, Exclude<string, "foo"> results in string.
    This proposal is logically connected with #4196. With a negating type, assuming Not<T>, the upper-bound could be transformed into a lower-bound with the help of never:

    type UpperToLower<T extends object> = Partial<T> & Record<string & Not<keyof T>, never>;

    So maybe this could be regarded as a sub-scenario to #4196.

  12. graphemecluster commented on Mar 12, 2023

    @graphemecluster
    Contributor

    Cross referencing #14520.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions