Skip to content

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

Closed

Description

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.

Activity

  1. added
    QuestionAn issue which isn't directly actionable in code
    SpecIssues related to the TypeScript language specification
    BugA bug in TypeScript
    on Dec 11, 2014
  2. JsonFreeman commented on Dec 11, 2014

    @JsonFreeman
    ContributorAuthor

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

  3. ahejlsberg commented on Dec 13, 2014

    @ahejlsberg
    Member

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

  4. added
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Dec 15, 2014
  5. JsonFreeman commented on Mar 23, 2015

    @JsonFreeman
    ContributorAuthor

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

  6. JsonFreeman commented on Jul 3, 2015

    @JsonFreeman
    ContributorAuthor

    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.

    Mohamed Hegazy (@mhegazy) Anders Hejlsberg (@ahejlsberg) Do you agree?

  7. ahejlsberg commented on Jul 3, 2015

    @ahejlsberg
    Member

    I agree, I think we can close this one.

  8. added
    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    on Jul 6, 2015
  9. added
    SuggestionAn idea for TypeScript
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    SpecIssues related to the TypeScript language specification
    on Jul 6, 2015
  10. added
    BugA bug in TypeScript
    FixedA PR has been merged for this issue
    and removed
    SuggestionAn idea for TypeScript
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it
    on Mar 20, 2017
  11. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions