Skip to content

Cannot use type assertion to Partial<T> where T is generic #19470

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

In #19467, @GregorioBR filed an issue about assigning a Foo to a Partial<T> where T extends Foo.

However, in trying to solve the issue with a type assertion, I got an error:

interface State {
    data?: string;
}

class View<S extends State> {

    public processState(): Partial<S> {
      return { data: "test" } as Partial<S>;
    }
}

This is surprising because I believe Partial<S> should be comparable to { data: "test" }.

Activity

added this to the TypeScript 2.7 milestone on Oct 25, 2017
mhegazy

mhegazy commented on Oct 25, 2017

@mhegazy
Contributor

@ahejlsberg we chatted about this issue earlier in the week. looking through the issues, there are multiple scenarios where this comes up, and our current behavior is not desired.

DanielRosenwasser

DanielRosenwasser commented on Oct 26, 2017

@DanielRosenwasser
MemberAuthor

I don't think it's the same issue, but it's definitely related. This is about making it easier to say "I know what I'm doing".

ahejlsberg

ahejlsberg commented on Dec 4, 2017

@ahejlsberg
Member

@DanielRosenwasser Looking at this, the original example compiles with no errors with the current master (and, it appears, has since 2.5) because the comparable relation is more permissive that the assignable relation.

@mhegazy The issues you reference above all have to do with assignability. Are you suggesting we change the rules there? If so, what would the new rules be?

mhegazy

mhegazy commented on Dec 4, 2017

@mhegazy
Contributor

I originally wanted to discuss the all as a group, and we did. I have since closed all assignebilty issues as working as intended.
It should be fine to close this one as fixed now.

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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

      Participants

      @DanielRosenwasser@ahejlsberg@mhegazy

      Issue actions

        Cannot use type assertion to `Partial<T>` where T is generic · Issue #19470 · microsoft/TypeScript