Skip to content

Functional Types / Interfaces: Return Type Widening #27237

Closed
@JasonGore

Description

@JasonGore

TypeScript Version: 3.0.1-insiders.20180726

Search Terms: function type interface return widening

This seems like an incredibly basic problem compared to others I've searched for so I apologize in advance if I'm missing some expected behavior with regards to functional types and interfaces.

Code

This code properly analyzes return type and generates an error when invalidKey is present.

type IArg = { testArg: number };
type IReturn = { validKey: number };

const generatesExpectedError = (props: IArg): IReturn => {
  return {
    validKey: 1,
    invalidKey: 2   // expected error
  };
};

However, when this declaration is elevated to a functional type or interface, return type appears to widen and this error no longer appears. Basic IReturn type checking seems to be present as there will be an error if validKey is removed. It's as if type checking on the return type has widened.

// Arrow Function Type
type IArrowFunction = (props: IArg) => IReturn;

const shouldGenerateError1: IArrowFunction = (props) => {
  return {
    validKey: 1,
    invalidKey: 2   // should error
  };
};
// Function Interface
interface IFunction {
  (a: IArg): IReturn;
}

const shouldGenerateError2: IFunction = function(props) {
  return {
    validKey: 1,
    invalidKey: 2   // should error
  };
};

Expected behavior:

Functional types and interfaces type check return value in the same manner as inline functional typing.

Actual behavior:

Functional types and interfaces do not have the same return type checking as inline functional typing.

Playground Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions