Skip to content

Commit

Permalink
Look at simplified types when checking distributive conditional const…
Browse files Browse the repository at this point in the history
…raints (#23884)
  • Loading branch information
weswigham authored May 4, 2018
1 parent df9e262 commit 618da24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6505,9 +6505,9 @@ namespace ts {
// over the conditional type and possibly reduced. For example, 'T extends undefined ? never : T'
// removes 'undefined' from T.
if (type.root.isDistributive) {
const constraint = getConstraintOfType(type.checkType);
const constraint = getConstraintOfType(getSimplifiedType(type.checkType));
if (constraint) {
const mapper = createTypeMapper([<TypeParameter>type.root.checkType], [constraint]);
const mapper = makeUnaryTypeMapper(type.root.checkType, constraint);
const instantiated = getConditionalTypeInstantiation(type, combineTypeMappers(mapper, type.mapper));
if (!(instantiated.flags & TypeFlags.Never)) {
return instantiated;
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/nonNullMappedType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [nonNullMappedType.ts]
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
const v: {} = p0[p1]!;
}

//// [nonNullMappedType.js]
"use strict";
function f(p0, p1) {
var v = p0[p1];
}
15 changes: 15 additions & 0 deletions tests/baselines/reference/nonNullMappedType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/nonNullMappedType.ts ===
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
>f : Symbol(f, Decl(nonNullMappedType.ts, 0, 0))
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))
>p0 : Symbol(p0, Decl(nonNullMappedType.ts, 0, 29))
>key : Symbol(key, Decl(nonNullMappedType.ts, 0, 36))
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))
>p1 : Symbol(p1, Decl(nonNullMappedType.ts, 0, 64))
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))

const v: {} = p0[p1]!;
>v : Symbol(v, Decl(nonNullMappedType.ts, 1, 9))
>p0 : Symbol(p0, Decl(nonNullMappedType.ts, 0, 29))
>p1 : Symbol(p1, Decl(nonNullMappedType.ts, 0, 64))
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/nonNullMappedType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/nonNullMappedType.ts ===
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
>f : <A extends string>(p0: { [key in A]: {} | undefined; }, p1: A) => void
>A : A
>p0 : { [key in A]: {} | undefined; }
>key : key
>A : A
>p1 : A
>A : A

const v: {} = p0[p1]!;
>v : {}
>p0[p1]! : NonNullable<{ [key in A]: {} | undefined; }[A]>
>p0[p1] : { [key in A]: {} | undefined; }[A]
>p0 : { [key in A]: {} | undefined; }
>p1 : A
}
4 changes: 4 additions & 0 deletions tests/cases/compiler/nonNullMappedType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @strict: true
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
const v: {} = p0[p1]!;
}

0 comments on commit 618da24

Please sign in to comment.