Skip to content

Feature Request: Naming an Implicit Return Type #13927

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

Closed
ricklove opened this issue Feb 7, 2017 · 1 comment
Closed

Feature Request: Naming an Implicit Return Type #13927

ricklove opened this issue Feb 7, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@ricklove
Copy link

ricklove commented Feb 7, 2017

I think it would be useful to be able to name a complex return type:

Code

export function createSomethingComplex() {
    return {
        thisIsComplex: (a: boolean, b: number, c: string) => ({ 'yes': { 'it': { 'is': true } } }),
        andIDontWantToWriteAnInterface: true
    };
}

// Option 1: Use additional return keyword
type ComplexThing = typeof return createSomethingComplex;

// Option 2: Use empty parameters
type ComplexThing = typeof createSomethingComplex();

Wordaround

It is possible to get the return type, by actually calling the method and then using that object to define the type:

let somethingComplex = createSomethingComplex();
type ComplexThing = typeof somethingComplex;

However, there are cases where it is not possible to call the function in order to get an object for cloning the return type.

Use Case

The use case I have found is when defining a utility function that uses another function's return type as a parameter.

For example, it is common in complex processes to break the main process into multiple functions that return an intermediate result of the process which is then passed to the next function to continue processing:

export function mainProcess() {
    let a = subProcessA();
    let b = subProcessB(a);
    let c = subProcessB(c);
    return c;
}

export function subProcessA() {
    return { a: true };
}

export function subProcessB(a: typeof return subProcessA){
    return { b: true };
}

export function subProcessC(a: typeof return subProcessB){
    return { c: true };
}

Of course these functions could be defined inline in the mainProcess, but for testing and code organization it may be ideal to export them at the base level.

It is also possible to manually define a type or interface for each, but in many cases, the return type value is it's own best definition.

@kube
Copy link

kube commented Feb 7, 2017

Already asked here: #6606

I agree this is a real pain, a lot of people are asking for this feature, and there's no news on this.

I just wrote a workaround that prevents you to have to write an interface:
https://github.com./kube/returnof

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 7, 2017
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants