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

Make a method of a generic interface A<T> available only if T is of a certain type #6529

Closed
thorn0 opened this issue Jan 18, 2016 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@thorn0
Copy link

thorn0 commented Jan 18, 2016

Let's look at the library Lodash for the example. Values of any type can be wrapped in _:

_({ a: 1 })
_([1,2,3])
_(x => x * x)

However, not all the methods of the wrapper make sense for any type of the wrapped value. For example, calling _(z),memoize() makes sense only if z is a function. So let's try to create a type definition for this situation:

interface Lodash {
    <T>(value: T): LodashWrapper<T>;
}
interface LodashWrapper<T> {
    memoize(): LodashWrapper<T>;
}

AFAIK for now it's not possible to write such a constraint for memoize that a call to it will type-check only when T is a function. Is it going to be possible in the future versions? Is there already an issue for this?

@DanielRosenwasser
Copy link
Member

You could make a subclass of LodashWrapper

interface LodashFunctionWrapper<T> extends LodashWrapper<T> {
    memoize(): LodashFunctionWrapper<T>;
}

and add an overload whose parameter is constrained to functions

interface Lodash {
    <T extends Function>(x: T): LodashFunctionWrapper<T>;
}

This is already done to some extent in DefinitelyTyped's declaration file for lodash.

By the way, while I can help out with this now, in the future you should really post questions like this to StackOverflow or our Gitter room.

Hope that helps!

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Jan 18, 2016
@thorn0
Copy link
Author

thorn0 commented Jan 18, 2016

I posted on SO, but didn't get an answer. And Lodash is just for example. My question is about a specific language feature, that's why I decided that it's okay to write here.

@thorn0
Copy link
Author

thorn0 commented Jan 18, 2016

And we can get LodashWrapper<Function> not only from a call to _. So we'll have to add an overload to all the other functions that return a wrapper if we use this approach.

@thorn0
Copy link
Author

thorn0 commented Jan 18, 2016

In the extreme, this feature could allow overloads like these:

interface Foo<T> {
    bar(): string if T extends Function;
    bar(): number; // otherwise
}

@thorn0 thorn0 changed the title Can I make a method of a generic interface A<T> available only if T is of a certain type? Make a method of a generic interface A<T> available only if T is of a certain type Jan 18, 2016
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Question An issue which isn't directly actionable in code labels Jan 18, 2016
@thorn0
Copy link
Author

thorn0 commented Jan 18, 2016

Turns out there is already an issue for this: #1290.

@thorn0 thorn0 closed this as completed Jan 18, 2016
@DanielRosenwasser DanielRosenwasser added Question An issue which isn't directly actionable in code and removed In Discussion Not yet reached consensus Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Jan 18, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

2 participants