Skip to content

Non-null assertion doesn't work for mapped types #23849

Description

@bowenni

TypeScript Version:
Reproducible in 2.7.2 and 2.9.0-dev.20180502.
Irreproducible in 2.6.2

Search Terms:
Mapped types, Non-null assertion

Code

function f<A extends string>(p0: {[key in A]: {}|undefined}, p1: A) {
  const v: {} = p0[p1]!;
}

Expected behavior:
No errors.

Actual behavior:
In 2.7.2:
error TS2322: Type '{ [key in A]: {} | undefined; }[A]' is not assignable to type '{}'.
Type '{} | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.

In 2.9.0-dev.20180502:
error TS2322: Type 'NonNullable<{ [key in A]: {} | undefined; }[A]>' is not assignable to type '{}'.
Type '{} | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.

Playground Link:
I cannot reproduce the error in the playground.
From command line, I'm running node_modules/.bin/tsc -strict test.ts to reproduce it.

Related Issues:
#19608

Rado Kirov (@rkirov)

Activity

  1. mhegazy commented on May 3, 2018

    @mhegazy
    Contributor

    Wesley Wigham (@weswigham) can you take a look.

  2. weswigham commented on May 3, 2018

    @weswigham
    Member

    I'm going to guess that when we look at NonNullable<{ [key in A]: {} | undefined; }[A]> and instantiate with the constraint and simplify to NonNullable<{} | undefined>, the process through which we do so is not creating a distributive conditional type (the original conditional was not distributed, and {} | undefined doesn't extend undefined, so just passes thru unchanged).

  3. bowenni commented on May 3, 2018

    @bowenni
    Author

    Another question is why this is irreproducible in the playground?

  4. weswigham commented on May 3, 2018

    @weswigham
    Member

    Bowen Ni (@bowenni) because as it turns out, this is a regression introduced by #23592, I believe (since, looking at the playground, it clearly worked in 2.8).

    Update: Our conditional type constraint comparison logic was incorrect, but not for the stated reason. The distributivity is handled correctly, but how we got the constraint for an indexed access is off. We saw {[K in A]: {} | undefined}[string] and just...decided not to simplify it and return no constraint because the object type was generic.

  5. bowenni commented on May 3, 2018

    @bowenni
    Author

    Thank you Wesley for the explanation!

  6. weswigham commented on May 4, 2018

    @weswigham
    Member

    And I've got a fix up at #23884 😉

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

Metadata

Metadata

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