Skip to content
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

Functional Types / Interfaces: Return Type Widening #27237

Closed
JasonGore opened this issue Sep 20, 2018 · 2 comments
Closed

Functional Types / Interfaces: Return Type Widening #27237

JasonGore opened this issue Sep 20, 2018 · 2 comments
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@JasonGore
Copy link
Member

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:

@RyanCavanaugh
Copy link
Member

Duplicate #241

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 20, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants