Skip to content

Commit 6bb16f1

Browse files
TypeScript Botandrewbranch
TypeScript Bot
andauthored
Cherry-pick PR #50620 into release-4.8 (#50676)
Component commits: 4635cdc Forward intersection state flag to conditional type target check Co-authored-by: Andrew Branch <[email protected]>
1 parent ca022ae commit 6bb16f1

5 files changed

+56
-2
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19788,8 +19788,8 @@ namespace ts {
1978819788
const skipTrue = !isTypeAssignableTo(getPermissiveInstantiation(c.checkType), getPermissiveInstantiation(c.extendsType));
1978919789
const skipFalse = !skipTrue && isTypeAssignableTo(getRestrictiveInstantiation(c.checkType), getRestrictiveInstantiation(c.extendsType));
1979019790
// TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't)
19791-
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false)) {
19792-
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
19791+
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
19792+
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
1979319793
if (result) {
1979419794
return result;
1979519795
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [excessPropertyCheckingIntersectionWithConditional.ts]
2+
type Foo<K> = K extends unknown ? { a: number } : unknown
3+
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
4+
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
5+
}
6+
7+
//// [excessPropertyCheckingIntersectionWithConditional.js]
8+
var createDefaultExample = function (x) {
9+
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts ===
2+
type Foo<K> = K extends unknown ? { a: number } : unknown
3+
>Foo : Symbol(Foo, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 0))
4+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 9))
5+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 9))
6+
>a : Symbol(a, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 35))
7+
8+
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
9+
>createDefaultExample : Symbol(createDefaultExample, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 5))
10+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
11+
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 34))
12+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
13+
>Foo : Symbol(Foo, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 0))
14+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
15+
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 51))
16+
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
17+
18+
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
19+
>a : Symbol(a, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 10))
20+
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 16))
21+
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 34))
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts ===
2+
type Foo<K> = K extends unknown ? { a: number } : unknown
3+
>Foo : Foo<K>
4+
>a : number
5+
6+
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
7+
>createDefaultExample : <K>(x: K) => Foo<K> & { x: K; }
8+
><K,>(x: K): Foo<K> & { x: K; } => { return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2} : <K>(x: K) => Foo<K> & { x: K; }
9+
>x : K
10+
>x : K
11+
12+
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
13+
>{ a: 1, x: x } : { a: number; x: K; }
14+
>a : number
15+
>1 : 1
16+
>x : K
17+
>x : K
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Foo<K> = K extends unknown ? { a: number } : unknown
2+
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
3+
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
4+
}

0 commit comments

Comments
 (0)