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

Generic constraint with type list #2099

Closed
NN--- opened this issue Feb 21, 2015 · 7 comments
Closed

Generic constraint with type list #2099

NN--- opened this issue Feb 21, 2015 · 7 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@NN---
Copy link

NN--- commented Feb 21, 2015

It is a generalization of #2049 .
I would like to see generic specifying concrete types:

// Can specify only A<number> or A<string>
interface A<T is number|string|Object> { }  

interface B<T is number|string> extends A<T> {} // number|string is subtype of number|string|Object .
@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 23, 2015
@RyanCavanaugh
Copy link
Member

In order to be considered, we need suggestions to have at least some motivating use cases, examples of desired usage, examples of things that would be / would not be errors and why, etc.. We don't have resources to ask clarifying questions teasing out what these things mean.

@NN---
Copy link
Author

NN--- commented Feb 23, 2015

One of use cases is the following:

interface KeyValueBase<K> {
  f(): K;
}

interface KeyValue<K is number|string, V> extends KeyValueBase<K> {
 [key: K]: V
}

The key type must be string or number , it is impossible to define this generic right now.
It can be possible to define it and use the type if the feature existed.

@DanielRosenwasser
Copy link
Member

You could also use a type alias.

type Key = number | string;

interface KeyValueBase {
  f(): Key;
}

interface KeyValue extends KeyValueBase {
 [key: Key]: V
}

@NN---
Copy link
Author

NN--- commented Feb 23, 2015

@DanielRosenwasser It is not the same since I don't know the exact type.
Also the indexer is one of possible samples.

@litera
Copy link

litera commented Jan 26, 2017

Object indexer is the perfect example and use case for is genetic type parameter constraint. This hasn't been updated in a while. Is there any progress?

@litera
Copy link

litera commented Jan 26, 2017

@DanielRosenwasser And btw even when using type aliasing TSLint still throws an error.

@massimonewsuk
Copy link

One issue I had is with Object.entries, but the type definition for that was pretty simple:

interface ObjectConstructor {
    entries<TValue>(object: { [key: string]: TValue }): [string, TValue][];
    entries<TValue>(object: { [key: number]: TValue }): [string, TValue][];
}

I understand it won't work for everything (objects that aren't strictly used as maps), so it would probably be better to just have your own "wrapper function" around Object.entries, but I thought I'd mention it in case it helps anyone else who finds this issue.

@microsoft microsoft locked and limited conversation to collaborators Jun 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants