Skip to content

Type assertion only works if one type is completely assignable to the other #1447

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

Closed
JsonFreeman opened this issue Dec 11, 2014 · 5 comments
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@JsonFreeman
Copy link
Contributor

JsonFreeman commented Dec 11, 2014

interface Foo {
    a?: string;
    b: string;
}
<Foo>{ a: undefined } // Neither type is assignable to the other

This happens because with respect to property a, Foo is the winner, but with respect to property b, the object literal is the winner (because b is missing). I think what the user expects is for us to mix and match the direction of assignability at all levels of nesting. So if each property is assignable in either direction, the check passes for that property. If the check passes for all the properties, the type assertion is allowed.

This would require work in the spec to add a new symmetric relation "is assignable to or from", which applies itself appropriately to nested types and other nested constructs.

@JsonFreeman JsonFreeman added Question An issue which isn't directly actionable in code Spec Issues related to the TypeScript language specification labels Dec 11, 2014
@JsonFreeman JsonFreeman added the Bug A bug in TypeScript label Dec 11, 2014
@JsonFreeman
Copy link
Contributor Author

Another potential fix is to copy optional properties into an object literal during contextual typing if they are missing.

@ahejlsberg
Copy link
Member

The right solution here is probably a new "is assignable to or from" relation as you suggest.

@mhegazy mhegazy added this to the TypeScript 1.6 milestone Dec 15, 2014
@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Dec 15, 2014
@JsonFreeman
Copy link
Contributor Author

Per issue #2441, we can use this for equality comparisons too.

@mhegazy mhegazy removed the Question An issue which isn't directly actionable in code label Apr 21, 2015
@mhegazy mhegazy removed the Bug A bug in TypeScript label May 27, 2015
@JsonFreeman
Copy link
Contributor Author

I don't think we should fix this issue. I think the fix is too costly and complex, and I don't think the issue is serious enough to warrant it.

@mhegazy @ahejlsberg Do you agree?

@ahejlsberg
Copy link
Member

I agree, I think we can close this one.

@JsonFreeman JsonFreeman added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it Too Complex An issue which adding support for may be too complex for the value it adds labels Jul 6, 2015
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Spec Issues related to the TypeScript language specification labels Jul 6, 2015
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript and removed Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it labels Mar 20, 2017
@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Mar 20, 2017
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants