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

Inferred return types checked against extended (as opposed to specific) declaration's return type. #30096

Closed
Hashbrown777 opened this issue Feb 26, 2019 · 2 comments
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@Hashbrown777
Copy link

Hashbrown777 commented Feb 26, 2019

TypeScript Version: typescript@3.4.0-dev.20190223

Search Terms: return types accept extra properties

Code

interface IOnlyBob {bob :string};

//this errors as we expect
//const _errors :IOnlyBob = {bob:'hi', steve:'there'};

//this works fine but should error
let _returnsBob :() => IOnlyBob;
_returnsBob = () => ({bob:'hi', steve:'there'});

//I'd expect this to be the way to do the above, if that's what someone actually wanted
let _returnsNotJustBob :<T extends IOnlyBob>() => T;
_returnsNotJustBob = () => ({bob:'hi', steve:'there'} as any); //have to do any, see https://github.com/Microsoft/TypeScript/issues/28154

Expected behavior:
Line 8 should fail.

Type '{ bob: string; steve: string; }' is not assignable to type 'IOnlyBob'.
  Object literal may only specify known properties, and 'steve' does not exist in type 'IOnlyBob'.

Actual behavior:
Works fine.

Playground Link: here

Related Issues:

@RyanCavanaugh
Copy link
Member

Duplicate / root-cause #241

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 27, 2019
@Hashbrown777
Copy link
Author

Oh ta! "Widening", I guess if I knew the lingo I'd've found that.
Thanks, I'll watch that space.

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