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

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

Closed
bowenni opened this issue May 3, 2018 · 6 comments
Closed

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

bowenni opened this issue May 3, 2018 · 6 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@bowenni
Copy link

bowenni commented May 3, 2018

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

@rkirov

@mhegazy
Copy link
Contributor

mhegazy commented May 3, 2018

@weswigham can you take a look.

@mhegazy mhegazy added the Bug A bug in TypeScript label May 3, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone May 3, 2018
@weswigham
Copy link
Member

weswigham commented May 3, 2018

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).

@bowenni
Copy link
Author

bowenni commented May 3, 2018

Another question is why this is irreproducible in the playground?

@weswigham
Copy link
Member

weswigham commented May 3, 2018

@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.

@bowenni
Copy link
Author

bowenni commented May 3, 2018

Thank you Wesley for the explanation!

@weswigham
Copy link
Member

weswigham commented May 4, 2018

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

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 4, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 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

3 participants