Skip to content

Commit

Permalink
Cherry-pick PR #50620 into release-4.8 (#50676)
Browse files Browse the repository at this point in the history
Component commits:
4635cdc Forward intersection state flag to conditional type target check

Co-authored-by: Andrew Branch <[email protected]>
  • Loading branch information
TypeScript Bot and andrewbranch authored Sep 7, 2022
1 parent ca022ae commit 6bb16f1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19788,8 +19788,8 @@ namespace ts {
const skipTrue = !isTypeAssignableTo(getPermissiveInstantiation(c.checkType), getPermissiveInstantiation(c.extendsType));
const skipFalse = !skipTrue && isTypeAssignableTo(getRestrictiveInstantiation(c.checkType), getRestrictiveInstantiation(c.extendsType));
// TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't)
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false)) {
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
if (result) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [excessPropertyCheckingIntersectionWithConditional.ts]
type Foo<K> = K extends unknown ? { a: number } : unknown
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
}

//// [excessPropertyCheckingIntersectionWithConditional.js]
var createDefaultExample = function (x) {
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts ===
type Foo<K> = K extends unknown ? { a: number } : unknown
>Foo : Symbol(Foo, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 0))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 9))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 9))
>a : Symbol(a, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 35))

const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
>createDefaultExample : Symbol(createDefaultExample, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 5))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 34))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
>Foo : Symbol(Foo, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 0, 0))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 51))
>K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30))

return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
>a : Symbol(a, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 10))
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 16))
>x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 34))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts ===
type Foo<K> = K extends unknown ? { a: number } : unknown
>Foo : Foo<K>
>a : number

const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
>createDefaultExample : <K>(x: K) => Foo<K> & { x: K; }
><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; }
>x : K
>x : K

return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
>{ a: 1, x: x } : { a: number; x: K; }
>a : number
>1 : 1
>x : K
>x : K
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Foo<K> = K extends unknown ? { a: number } : unknown
const createDefaultExample = <K,>(x: K): Foo<K> & { x: K; } => {
return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2
}

0 comments on commit 6bb16f1

Please sign in to comment.