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

Declaring generic type of implicitly called method #15877

Closed
chpoit opened this issue May 16, 2017 · 9 comments
Closed

Declaring generic type of implicitly called method #15877

chpoit opened this issue May 16, 2017 · 9 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@chpoit
Copy link

chpoit commented May 16, 2017

This is a feature request. I am unaware of any recent similar requests
For typing purposes and simple code, it would be good to be able to declare generics of implicitly called methods.

TypeScript Version: Any under 2.3.x at time of writing

Code Example
Take the following method

myImplicitlyCalledMethod = <T>(someVar: any): T => {
    let b:T;
    //Code that converts someVar to T or create an object with type T from someVar
    return b;
}

Actual behavior:
Right now when you call a method with a generic type implicitly, you cannot declare the type directly.
The following code is what I have to do to get the type, along with an implicit call of the method.

myCallingMethodWithoutTyping = (someVar:any) => this.myImplicitlyCalledMethod;
myCallingMethodWithTyping = (someVar:any) => this.myImplicitlyCalledMethod<number>(someVar);

Expected behavior:
This is what I would like to be able to do :

myCallingMethodWithoutTyping = (someVar:any) => this.myImplicitlyCalledMethod<number>;

The code however throws an exception while compiling as it expects you to pass the parameter when you declare the type of the generic.

Language feature checklist

  • Syntactic: Allow declaring of generics in implicitly called methods
  • Semantic: Should not change the core logic of typescript, or anything.
  • Emit: Emitted code would not change, it would simply be to help with declaring types with less code.
  • Compatibility: Code using this wouldn't be backwards compatible.
  • Other:
    • Performance: This might affect performance depending on how it's implemented.
    • Tooling: Would actually help with inferred typing
@aluanhaddad
Copy link
Contributor

Definitely interesting. Do you see this extending to other kinds of values?

@mhegazy mhegazy added Bug A bug in TypeScript and removed Bug A bug in TypeScript labels May 17, 2017
@chpoit
Copy link
Author

chpoit commented May 17, 2017

I'm unsure what you mean by value, by my main usecase would be to use through Rxjs observables in angular to reduce the amount of what I consider to be useless code, for examble being a simple api call going from :

this.http.get(url, options)
    .map((res)=>this.extractApiResponse<someType>(res))
    .catch(this.handleError);

to :

this.http.get(url, options)
    .map(this.extractApiResponse<someType>)
    .catch(this.handleError);

Which I personally find better, and would allow me to do as much with less code. Granted it's not much code, but when you write a slight variation of this 2-5 times a day it gets tedious.
I guess to some extent it's laziness, but I also find the second example would allow for less "useless" code.

@mhegazy mhegazy added the Suggestion An idea for TypeScript label May 18, 2017
@billba
Copy link
Member

billba commented May 24, 2017

The expected behavior I personally am looking for is around functions, not methods, but same idea applies:

const myCallingFunctionWithoutTyping = myImplicitlyCalledFunction<any>;
const myCallingFunctionWithTyping = myImplicitlyCalledFunction<number>;

In my head I sometimes try to replace const with something resembling a type alias, because of course there's no reason we need to create two variables whose whole purpose in life is to narrow the typing of an existing function:

alias myCallingFunctionWithoutTyping = myImplicitlyCalledFunction<any>;
alias myCallingFunctionWithTyping = myImplicitlyCalledFunction<number>;

But in the end I'd be happy with just making them variables, because it solves the problem.

@davetayls
Copy link

This seems to have gone quiet for a while. Is there any news as to whether this is planned for being implemented?

Thanks

@RyanCavanaugh RyanCavanaugh added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Mar 23, 2018
@RyanCavanaugh
Copy link
Member

There is a lot to be worked out here before it'd be actionable. Functions can be overloaded, and generics can have defaults, so it's not entirely clear what f<T> means for all possible definitions of f

@pelotom
Copy link

pelotom commented Mar 23, 2018

As far as overloading, it seems like f<T> should mean that the set of overloads is filtered to just those for which a single type parameter of type T is acceptable, and then the parameter is removed and all instances of it replaced with T. So an overloaded function becomes a less overloaded, more specialized overloaded function.

@maciejw
Copy link

maciejw commented Jun 18, 2018

there was an issue about generic namespaces #19728 but it was closed in favor of this one, so I have interesting case I think. namespaces are interesting in context of generics, because namespaces can hold types, like type aliases. but I thing that what I want to achieve could be awkward with namespaces basically I want o have a type "constructor", or template, that is in types realm not values realm.

namespace X<T> {
  type Y = Record<T, string>
  type Z = Record<T, boolean>
}

this also is interesting

type X<T> = {
  type Y = Record<T, string>
  type Z = Record<T, boolean>
}

then I could do

type XX = X<'foo'>

and then later I could use

function f(y: XX.Y, z: XX.Z ){}

what do you think about it?

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Aug 22, 2018
@RyanCavanaugh
Copy link
Member

I think this would reasonably be subsumed by #17574 so doesn't need to exist as a one-off feature that'd be almost the same amount of work for a lot less value

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Too Complex" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

9 participants