Skip to content

Return types of arrow functions allow additional properties. #41850

Closed
@ChristophGr

Description

@ChristophGr

Returned objects of arrow functions can have more properties than the type specified in the variable declaration .

TypeScript Version: 4.1.2 / 4.2.0-dev.20201207

Search Terms:
arrow function return type covariant additional properties

Code

type MyResult = { foo: string };

const arrowProvider: () => MyResult = () => ({ foo: 'bar', bar: 'foo' });
// no error


function funcProvider(): MyResult {
  return { foo: 'bar', bar: 'foo' }; // error
}

Expected behavior:
Both funcProvider and arrowProvider should not compile because of the superfluous property bar in there.

Actual behavior:
Only funcProvider triggers a compile-error

Playground Link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiBKEDOBXANsKBeKBvKAZgPZEBcUSwATgJYB2A5lAL4DcAUOwMZF2VQBDKlSIB3AAoiAbjQAmEKuQAUASmwA+WAmTpMOVRqhL8xMlADkAIyHmANFGuKLp8yxUd2BFHS7AavQm8uSSIZeSpVcjhEVAw8digoKghgFCo6PEIScisbe0cclxYOZiA

Related Issues:

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