Skip to content

FR: add config option to force return types #18529

@QwertyZW

Description

@QwertyZW

There's an option to configure the compiler to force return types in function parameters, but not the return types of the function themselves.

I would like, with a compiler configuration option, to be able to not allow this to compile

function myAddFunction(a : number, b : number) {
    let myIntendedReturn = a + b;
    return "surprise return type haHA";
}

Initially I had thought that the compiler option: noImplicitAny would serve this purpose. But it does not.

Reasons why I would like something like this:

  1. solidify the return type of the function in its declaration
  2. allows for code to be analyzed statically by IDEs/editors to provide better autocompletion/correctness checking

For example consider we have these following functions in our symbol autocompletion list

function myNumberAdder(a: number, b : number){
    return a + b;
}

function myStringAdder(a : string, b :string){
    return `${a}${b}`
}

If I was to type

let sum : number = my// here the IDE/editor will know to suggest myNumberAdder as opposed to myStringAdder, which also serves as a check for correctness

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionOut of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions