Skip to content

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

Closed

Description

@Hashbrown777

TypeScript Version: [email protected]

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:

Activity

  1. RyanCavanaugh commented on Feb 27, 2019

    @RyanCavanaugh
    Member

    Duplicate / root-cause #241

  2. Hashbrown777 commented on Feb 27, 2019

    @Hashbrown777
    Author

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

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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