Skip to content

Feature Request: Naming an Implicit Return Type #13927

@ricklove

Description

@ricklove

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions