Skip to content

Mention "return type" in the error message when return type of a function is not matching the declared typeΒ #51449

Open
@mohsen1

Description

@mohsen1

Suggestion

πŸ” Search Terms

return type, error message

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Mention "return type" when return type of a function is not matching the declared type

When a parameter type is not matching the declared type, the error message has the words "parameter X" in it which makes it super easy to spot the error. For return types it only says type X is not compatible with type Y but is not clear that it is the return type

πŸ“ƒ Motivating Example

Playground

const func: (a: string, very: number, confusing: string, error: boolean, message: string) => string = 
    (a: string, very: number, confusing: string, error: boolean, message: string) => {
        return 1
    }

Error:

Type '(a: string, very: number, confusing: string, error: boolean, message: string) => number' is not assignable to type '(a: string, very: number, confusing: string, error: boolean, message: string) => string'.
  Type 'number' is not assignable to type 'string'.(2322)

πŸ’» Use Cases

This can get tricky in very large and complex types. Just mentioning the word "return" can save a ton of time debugging issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions