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

Typed function allows returning of objects with unknown properties #20718

Closed
mpk opened this issue Dec 15, 2017 · 2 comments
Closed

Typed function allows returning of objects with unknown properties #20718

mpk opened this issue Dec 15, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mpk
Copy link

mpk commented Dec 15, 2017

TypeScript Version: 2.6.2

Code

interface TestInterface {
    testValue: boolean;
}

let testFunc: () => TestInterface;

// Compiler allows returning unknown properties
testFunc = function() {
    return { testValue: true, otherValue: 7 };
}

// When the return type is explicitly specified, compiler warns about unknown properties
testFunc = function(): TestInterface {
    return { testValue: true, otherValue: 8 };
}

Playground

Expected behavior:
Since the testFunc has type annotation that specifies return value TestInterface, I expect that I will be able to return only objects with properties specified in TestInterface. However, the compiler allows me to return properties not specified in TestInterface.

To achieve my expected behavior, I need to explicitly specify the function return value. I think the compiler should be able to infer this from the type annotation.

Actual behavior:
Compiler does not complain about unknown object properties.

@j-oliveras
Copy link
Contributor

@RyanCavanaugh
Copy link
Member

Duplicate of #7547

@RyanCavanaugh RyanCavanaugh marked this as a duplicate of #7547 Dec 15, 2017
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 15, 2017
@mpk mpk closed this as completed Dec 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 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

3 participants