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

Return type of generic function interface allows object literals to have unknown properties. #18020

Closed
shaunluttin opened this issue Aug 24, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@shaunluttin
Copy link

shaunluttin commented Aug 24, 2017

TypeScript Version: 2.4.0

Code

type MyState = { foo: number; };
type Reducer<T> = (state: T) => T;

const reducer1: Reducer<MyState> = (state) => {
  return { foo: 123, bar: 123 }; // works.
};

const reducer2 = (state: MyState) : MyState  => {
  return { foo: 123, bar: 123 }; // fails.
  
  // Type '{ foo: number; bar: number; }' is not 
  // assignable to type 'MyState'.
  // Object literal may only specify known properties, 
  // and 'bar' does not exist in type 'MyState'.
};

Expected behavior:

The implementations for reducer1 and reducer2 have the same behavior.

Actual behavior:

The implementations for reducer1 and reducer2 have different behavior.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 24, 2017

Please see #7547

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 24, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants