Skip to content

Look at simplified types when checking distributive conditional constraints #23884

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6501,9 +6501,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]!;
}