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

Cannot assert Partial<T>[keyof T] not to be undefined using ! (regression from 2.5) #20673

Closed
mprobst opened this issue Dec 13, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@mprobst
Copy link
Contributor

mprobst commented Dec 13, 2017

TypeScript Version: 2.6.2

Code

See also in the playground example here.

function createComponent<T>(
    comp: T, inputs: Partial<T> = {}) {
    for (const key in inputs) {
        if (inputs.hasOwnProperty(key)) {
            const partialVal = inputs[key];
            const value = partialVal!;
            comp[key] = value;
        }
    }
}

Expected behavior:

parialVal! should strip the |undefined from value, and the assignment should succeed.

Actual behavior:

  Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
    Type 'undefined' is not assignable to type 'T[keyof T]'.
      Type 'undefined' is not assignable to type 'T[string]'.
        Type 'Partial<T>[keyof T]' is not assignable to type 'T[string]'.
          Type 'T[keyof T] | undefined' is not assignable to type 'T[string]'.
            Type 'undefined' is not assignable to type 'T[string]'.
@ghost
Copy link

ghost commented Dec 13, 2017

I think this would essentially require us to have #14366 working so we could have a NonNull<Partial<T>[keyof T]> type.

@mprobst
Copy link
Contributor Author

mprobst commented Dec 13, 2017

For the record, this used to succeed in 2.5, so this is a regression.

@mprobst mprobst changed the title Cannot assert Partial<T>[keyof T] not to be undefined using ! Cannot assert Partial<T>[keyof T] not to be undefined using ! (regression from 2.5) Dec 13, 2017
@ghost
Copy link

ghost commented Dec 13, 2017

OK, I must be wrong about a NonNull type being needed.

@ghost ghost added the Bug A bug in TypeScript label Dec 13, 2017
@ahejlsberg
Copy link
Member

ahejlsberg commented Dec 13, 2017

This is caused by #18042. Previously we would immediately transform an indexed access applied to a mapped type { [P in keyof T]?: T[P] | undefined }[X] into a simpler form T[X] | undefined, but we now defer that operation. For that reason the type appears to not include undefined and the ! operator has no effect.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Jan 10, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jan 10, 2018

duplicate of #14366

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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

4 participants