Look at simplified types when checking distributive conditional constraints (#23884)

This commit is contained in:
Wesley Wigham 2018-05-04 14:35:39 -07:00 committed by GitHub
parent df9e2621d2
commit 618da24d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 2 deletions

View File

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

View File

@ -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];
}

View File

@ -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))
}

View File

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

View File

@ -0,0 +1,4 @@
// @strict: true
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
const v: {} = p0[p1]!;
}