Skip to content

Commit 618da24

Browse files
authored
Look at simplified types when checking distributive conditional constraints (#23884)
1 parent df9e262 commit 618da24

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

Diff for: src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6505,9 +6505,9 @@ namespace ts {
65056505
// over the conditional type and possibly reduced. For example, 'T extends undefined ? never : T'
65066506
// removes 'undefined' from T.
65076507
if (type.root.isDistributive) {
6508-
const constraint = getConstraintOfType(type.checkType);
6508+
const constraint = getConstraintOfType(getSimplifiedType(type.checkType));
65096509
if (constraint) {
6510-
const mapper = createTypeMapper([<TypeParameter>type.root.checkType], [constraint]);
6510+
const mapper = makeUnaryTypeMapper(type.root.checkType, constraint);
65116511
const instantiated = getConditionalTypeInstantiation(type, combineTypeMappers(mapper, type.mapper));
65126512
if (!(instantiated.flags & TypeFlags.Never)) {
65136513
return instantiated;

Diff for: tests/baselines/reference/nonNullMappedType.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [nonNullMappedType.ts]
2+
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
3+
const v: {} = p0[p1]!;
4+
}
5+
6+
//// [nonNullMappedType.js]
7+
"use strict";
8+
function f(p0, p1) {
9+
var v = p0[p1];
10+
}

Diff for: tests/baselines/reference/nonNullMappedType.symbols

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/nonNullMappedType.ts ===
2+
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
3+
>f : Symbol(f, Decl(nonNullMappedType.ts, 0, 0))
4+
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))
5+
>p0 : Symbol(p0, Decl(nonNullMappedType.ts, 0, 29))
6+
>key : Symbol(key, Decl(nonNullMappedType.ts, 0, 36))
7+
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))
8+
>p1 : Symbol(p1, Decl(nonNullMappedType.ts, 0, 64))
9+
>A : Symbol(A, Decl(nonNullMappedType.ts, 0, 11))
10+
11+
const v: {} = p0[p1]!;
12+
>v : Symbol(v, Decl(nonNullMappedType.ts, 1, 9))
13+
>p0 : Symbol(p0, Decl(nonNullMappedType.ts, 0, 29))
14+
>p1 : Symbol(p1, Decl(nonNullMappedType.ts, 0, 64))
15+
}

Diff for: tests/baselines/reference/nonNullMappedType.types

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/nonNullMappedType.ts ===
2+
function f<A extends string>(p0: { [key in A]: {} | undefined }, p1: A) {
3+
>f : <A extends string>(p0: { [key in A]: {} | undefined; }, p1: A) => void
4+
>A : A
5+
>p0 : { [key in A]: {} | undefined; }
6+
>key : key
7+
>A : A
8+
>p1 : A
9+
>A : A
10+
11+
const v: {} = p0[p1]!;
12+
>v : {}
13+
>p0[p1]! : NonNullable<{ [key in A]: {} | undefined; }[A]>
14+
>p0[p1] : { [key in A]: {} | undefined; }[A]
15+
>p0 : { [key in A]: {} | undefined; }
16+
>p1 : A
17+
}

Diff for: tests/cases/compiler/nonNullMappedType.ts

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

0 commit comments

Comments
 (0)