Skip to content

Commit 36dc04b

Browse files
authored
Merge pull request #28718 from Microsoft/fixDefinitelyAssignableRelation
Fix definitely assignable relation
2 parents 9319ea4 + ff456ab commit 36dc04b

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12259,7 +12259,8 @@ namespace ts {
1225912259
else if (target.flags & TypeFlags.IndexedAccess) {
1226012260
// A type S is related to a type T[K], where T and K aren't both type variables, if S is related to C,
1226112261
// where C is the base constraint of T[K]
12262-
if (relation !== identityRelation && !(isGenericObjectType((<IndexedAccessType>target).objectType) && isGenericIndexType((<IndexedAccessType>target).indexType))) {
12262+
if (relation !== identityRelation && relation !== definitelyAssignableRelation &&
12263+
!(isGenericObjectType((<IndexedAccessType>target).objectType) && isGenericIndexType((<IndexedAccessType>target).indexType))) {
1226312264
const constraint = getBaseConstraintOfType(target);
1226412265
if (constraint && constraint !== target) {
1226512266
if (result = isRelatedTo(source, constraint, reportErrors)) {

tests/baselines/reference/conditionalTypes2.errors.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,13 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
203203

204204
type C2<T, V, E> =
205205
T extends object ? { [Q in keyof T]: C2<T[Q], V, E>; } : T;
206+
207+
// Repro from #28654
208+
209+
type MaybeTrue<T extends { b: boolean }> = true extends T["b"] ? "yes" : "no";
210+
211+
type T0 = MaybeTrue<{ b: never }> // "no"
212+
type T1 = MaybeTrue<{ b: false }>; // "no"
213+
type T2 = MaybeTrue<{ b: true }>; // "yes"
214+
type T3 = MaybeTrue<{ b: boolean }>; // "yes"
206215

tests/baselines/reference/conditionalTypes2.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ type B2<T, V> =
145145

146146
type C2<T, V, E> =
147147
T extends object ? { [Q in keyof T]: C2<T[Q], V, E>; } : T;
148+
149+
// Repro from #28654
150+
151+
type MaybeTrue<T extends { b: boolean }> = true extends T["b"] ? "yes" : "no";
152+
153+
type T0 = MaybeTrue<{ b: never }> // "no"
154+
type T1 = MaybeTrue<{ b: false }>; // "no"
155+
type T2 = MaybeTrue<{ b: true }>; // "yes"
156+
type T3 = MaybeTrue<{ b: boolean }>; // "yes"
148157

149158

150159
//// [conditionalTypes2.js]
@@ -304,3 +313,18 @@ declare type B2<T, V> = T extends object ? T extends any[] ? T : {
304313
declare type C2<T, V, E> = T extends object ? {
305314
[Q in keyof T]: C2<T[Q], V, E>;
306315
} : T;
316+
declare type MaybeTrue<T extends {
317+
b: boolean;
318+
}> = true extends T["b"] ? "yes" : "no";
319+
declare type T0 = MaybeTrue<{
320+
b: never;
321+
}>;
322+
declare type T1 = MaybeTrue<{
323+
b: false;
324+
}>;
325+
declare type T2 = MaybeTrue<{
326+
b: true;
327+
}>;
328+
declare type T3 = MaybeTrue<{
329+
b: boolean;
330+
}>;

tests/baselines/reference/conditionalTypes2.symbols

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,31 @@ type C2<T, V, E> =
551551
>E : Symbol(E, Decl(conditionalTypes2.ts, 144, 13))
552552
>T : Symbol(T, Decl(conditionalTypes2.ts, 144, 8))
553553

554+
// Repro from #28654
555+
556+
type MaybeTrue<T extends { b: boolean }> = true extends T["b"] ? "yes" : "no";
557+
>MaybeTrue : Symbol(MaybeTrue, Decl(conditionalTypes2.ts, 145, 63))
558+
>T : Symbol(T, Decl(conditionalTypes2.ts, 149, 15))
559+
>b : Symbol(b, Decl(conditionalTypes2.ts, 149, 26))
560+
>T : Symbol(T, Decl(conditionalTypes2.ts, 149, 15))
561+
562+
type T0 = MaybeTrue<{ b: never }> // "no"
563+
>T0 : Symbol(T0, Decl(conditionalTypes2.ts, 149, 78))
564+
>MaybeTrue : Symbol(MaybeTrue, Decl(conditionalTypes2.ts, 145, 63))
565+
>b : Symbol(b, Decl(conditionalTypes2.ts, 151, 21))
566+
567+
type T1 = MaybeTrue<{ b: false }>; // "no"
568+
>T1 : Symbol(T1, Decl(conditionalTypes2.ts, 151, 33))
569+
>MaybeTrue : Symbol(MaybeTrue, Decl(conditionalTypes2.ts, 145, 63))
570+
>b : Symbol(b, Decl(conditionalTypes2.ts, 152, 21))
571+
572+
type T2 = MaybeTrue<{ b: true }>; // "yes"
573+
>T2 : Symbol(T2, Decl(conditionalTypes2.ts, 152, 34))
574+
>MaybeTrue : Symbol(MaybeTrue, Decl(conditionalTypes2.ts, 145, 63))
575+
>b : Symbol(b, Decl(conditionalTypes2.ts, 153, 21))
576+
577+
type T3 = MaybeTrue<{ b: boolean }>; // "yes"
578+
>T3 : Symbol(T3, Decl(conditionalTypes2.ts, 153, 33))
579+
>MaybeTrue : Symbol(MaybeTrue, Decl(conditionalTypes2.ts, 145, 63))
580+
>b : Symbol(b, Decl(conditionalTypes2.ts, 154, 21))
581+

tests/baselines/reference/conditionalTypes2.types

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,28 @@ type C2<T, V, E> =
341341

342342
T extends object ? { [Q in keyof T]: C2<T[Q], V, E>; } : T;
343343

344+
// Repro from #28654
345+
346+
type MaybeTrue<T extends { b: boolean }> = true extends T["b"] ? "yes" : "no";
347+
>MaybeTrue : MaybeTrue<T>
348+
>b : boolean
349+
>true : true
350+
351+
type T0 = MaybeTrue<{ b: never }> // "no"
352+
>T0 : "no"
353+
>b : never
354+
355+
type T1 = MaybeTrue<{ b: false }>; // "no"
356+
>T1 : "no"
357+
>b : false
358+
>false : false
359+
360+
type T2 = MaybeTrue<{ b: true }>; // "yes"
361+
>T2 : "yes"
362+
>b : true
363+
>true : true
364+
365+
type T3 = MaybeTrue<{ b: boolean }>; // "yes"
366+
>T3 : "yes"
367+
>b : boolean
368+

tests/cases/conformance/types/conditional/conditionalTypes2.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,12 @@ type B2<T, V> =
147147

148148
type C2<T, V, E> =
149149
T extends object ? { [Q in keyof T]: C2<T[Q], V, E>; } : T;
150+
151+
// Repro from #28654
152+
153+
type MaybeTrue<T extends { b: boolean }> = true extends T["b"] ? "yes" : "no";
154+
155+
type T0 = MaybeTrue<{ b: never }> // "no"
156+
type T1 = MaybeTrue<{ b: false }>; // "no"
157+
type T2 = MaybeTrue<{ b: true }>; // "yes"
158+
type T3 = MaybeTrue<{ b: boolean }>; // "yes"

0 commit comments

Comments
 (0)