Skip to content

Object returned from a function can have keys not present in its return type #50064

Closed
@Dwlad90

Description

@Dwlad90

Bug Report

When a function is typed to return an object of a certain type, the properties of the object can be expanded, meaning extra properties can be added to the ones described in the type or interface.

🔎 Search Terms

function return object type

🕗 Version & Regression Information

This is observable on the playground in all available versions.

Couldn't find anything I understood to be relevant in https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

  • This is a crash: No
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about interfaces and types of function return values

⏯ Playground Link

Buggy example on @next

💻 Code

interface Result {
    foo: string;
}

type Func = () => Result

const noError:Func = () => ({
  foo: 'string1',
  // @ts-expect-error
  bar: 2,
});

const error:Func = (): Result => ({
  foo: 'string1',
  // @ts-expect-error
  bar: 2,
});

const error2:Func = (): ReturnType<Func> => ({
  foo: 'string1',
  // @ts-expect-error
  bar: 2,
});

🙁 Actual behavior

Extra properties in the returned object, which aren't defined in the interface or type, do not raise an error.

🙂 Expected behavior

Extra properties in the returned object, which aren't defined in the interface or type, should raise an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions