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

Another inference bug #3067

Closed
zpdDG4gta8XKpMCd opened this issue May 7, 2015 · 3 comments
Closed

Another inference bug #3067

zpdDG4gta8XKpMCd opened this issue May 7, 2015 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@zpdDG4gta8XKpMCd
Copy link

Not sure if this one relates to #3038

interface A {
    x: string;
}

interface B {
    x: string;
    y: string;
}

function copyB(value: B): B {
    return undefined;
}

var values: A[] = [];

values.map(value => copyB(value)) // fails as expected
values.map(copyB); // <-- expected to fail, but it does not

Can seen at playground: link

@ahejlsberg
Copy link
Member

That is by design. For purposes of assignment compatibility, parameters in signatures are bi-variant. We do this more by necessity than choice. There is good discussion of the issue in #1394. As @RyanCavanaugh points out in that thread, without this rule Array<Dog> wouldn't be a structural subtype of Array<Animal>.

@ahejlsberg ahejlsberg added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label May 7, 2015
@zpdDG4gta8XKpMCd
Copy link
Author

@ahejlsberg, this is a very sad news to me, we are not using array methods (they are commented out in our lib.d.ts), is there a way to detect this pattern in code using compiler API?

@danquirk
Copy link
Member

danquirk commented May 8, 2015

@Aleksey-Bykov See #274. It's definitely possible to change the compiler's behavior to disallow this, and we've considered it already as you can see in that issue. It would simply need to be opt in behavior or else it breaks too much existing code (including core JS idioms).

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants