Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Upper-bound generic type constraints #9252

Open
GregRos opened this issue Jun 19, 2016 · 9 comments
Open

Suggestion: Upper-bound generic type constraints #9252

GregRos opened this issue Jun 19, 2016 · 9 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@GregRos
Copy link

GregRos commented Jun 19, 2016

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>
@Strate
Copy link

Strate commented Jun 19, 2016

Possible duplicate of
#4889
#371

@GregRos
Copy link
Author

GregRos commented Jun 20, 2016

@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.

@Strate
Copy link

Strate commented Jun 20, 2016

@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.

@GregRos
Copy link
Author

GregRos commented Jun 20, 2016

@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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 10, 2016

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

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 10, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed Duplicate An existing issue was already created labels Oct 9, 2018
@microsoft microsoft unlocked this conversation Oct 9, 2018
@RyanCavanaugh RyanCavanaugh reopened this Oct 9, 2018
@timsuchanek
Copy link
Contributor

@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 🙏

@laurensdijkstra
Copy link

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

How could the previous example from @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)

@truemogician
Copy link

truemogician commented Nov 6, 2022

@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.

@graphemecluster
Copy link
Contributor

Cross referencing #14520.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants