Skip to content

Declaring generic type of implicitly called method  #15877

Closed as not planned
Closed as not planned
@chpoit

Description

@chpoit

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions