-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix isTypeDerivedFrom to properly handle
{}
and intersections (#51631)
* Fix isTypeDerivedFrom to properly handle {} and intersections * Add tests
- Loading branch information
1 parent
c460e7e
commit cee6366
Showing
10 changed files
with
571 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//// [inKeywordAndIntersection.ts] | ||
class A { a = 0 } | ||
class B { b = 0 } | ||
|
||
function f10(obj: A & { x: string } | B) { | ||
if (obj instanceof Object) { | ||
obj; // A & { x: string } | B | ||
} | ||
else { | ||
obj; // Error | ||
} | ||
} | ||
|
||
// Repro from #50844 | ||
|
||
interface InstanceOne { | ||
one(): void | ||
} | ||
|
||
interface InstanceTwo { | ||
two(): void | ||
} | ||
|
||
const instance = {} as InstanceOne | InstanceTwo | ||
|
||
const ClassOne = {} as { new(): InstanceOne } & { foo: true }; | ||
|
||
if (instance instanceof ClassOne) { | ||
instance.one(); | ||
} | ||
|
||
|
||
//// [inKeywordAndIntersection.js] | ||
"use strict"; | ||
var A = /** @class */ (function () { | ||
function A() { | ||
this.a = 0; | ||
} | ||
return A; | ||
}()); | ||
var B = /** @class */ (function () { | ||
function B() { | ||
this.b = 0; | ||
} | ||
return B; | ||
}()); | ||
function f10(obj) { | ||
if (obj instanceof Object) { | ||
obj; // A & { x: string } | B | ||
} | ||
else { | ||
obj; // Error | ||
} | ||
} | ||
var instance = {}; | ||
var ClassOne = {}; | ||
if (instance instanceof ClassOne) { | ||
instance.one(); | ||
} |
65 changes: 65 additions & 0 deletions
65
tests/baselines/reference/inKeywordAndIntersection.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
=== tests/cases/compiler/inKeywordAndIntersection.ts === | ||
class A { a = 0 } | ||
>A : Symbol(A, Decl(inKeywordAndIntersection.ts, 0, 0)) | ||
>a : Symbol(A.a, Decl(inKeywordAndIntersection.ts, 0, 9)) | ||
|
||
class B { b = 0 } | ||
>B : Symbol(B, Decl(inKeywordAndIntersection.ts, 0, 17)) | ||
>b : Symbol(B.b, Decl(inKeywordAndIntersection.ts, 1, 9)) | ||
|
||
function f10(obj: A & { x: string } | B) { | ||
>f10 : Symbol(f10, Decl(inKeywordAndIntersection.ts, 1, 17)) | ||
>obj : Symbol(obj, Decl(inKeywordAndIntersection.ts, 3, 13)) | ||
>A : Symbol(A, Decl(inKeywordAndIntersection.ts, 0, 0)) | ||
>x : Symbol(x, Decl(inKeywordAndIntersection.ts, 3, 23)) | ||
>B : Symbol(B, Decl(inKeywordAndIntersection.ts, 0, 17)) | ||
|
||
if (obj instanceof Object) { | ||
>obj : Symbol(obj, Decl(inKeywordAndIntersection.ts, 3, 13)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
obj; // A & { x: string } | B | ||
>obj : Symbol(obj, Decl(inKeywordAndIntersection.ts, 3, 13)) | ||
} | ||
else { | ||
obj; // Error | ||
>obj : Symbol(obj, Decl(inKeywordAndIntersection.ts, 3, 13)) | ||
} | ||
} | ||
|
||
// Repro from #50844 | ||
|
||
interface InstanceOne { | ||
>InstanceOne : Symbol(InstanceOne, Decl(inKeywordAndIntersection.ts, 10, 1)) | ||
|
||
one(): void | ||
>one : Symbol(InstanceOne.one, Decl(inKeywordAndIntersection.ts, 14, 23)) | ||
} | ||
|
||
interface InstanceTwo { | ||
>InstanceTwo : Symbol(InstanceTwo, Decl(inKeywordAndIntersection.ts, 16, 1)) | ||
|
||
two(): void | ||
>two : Symbol(InstanceTwo.two, Decl(inKeywordAndIntersection.ts, 18, 23)) | ||
} | ||
|
||
const instance = {} as InstanceOne | InstanceTwo | ||
>instance : Symbol(instance, Decl(inKeywordAndIntersection.ts, 22, 5)) | ||
>InstanceOne : Symbol(InstanceOne, Decl(inKeywordAndIntersection.ts, 10, 1)) | ||
>InstanceTwo : Symbol(InstanceTwo, Decl(inKeywordAndIntersection.ts, 16, 1)) | ||
|
||
const ClassOne = {} as { new(): InstanceOne } & { foo: true }; | ||
>ClassOne : Symbol(ClassOne, Decl(inKeywordAndIntersection.ts, 24, 5)) | ||
>InstanceOne : Symbol(InstanceOne, Decl(inKeywordAndIntersection.ts, 10, 1)) | ||
>foo : Symbol(foo, Decl(inKeywordAndIntersection.ts, 24, 49)) | ||
|
||
if (instance instanceof ClassOne) { | ||
>instance : Symbol(instance, Decl(inKeywordAndIntersection.ts, 22, 5)) | ||
>ClassOne : Symbol(ClassOne, Decl(inKeywordAndIntersection.ts, 24, 5)) | ||
|
||
instance.one(); | ||
>instance.one : Symbol(InstanceOne.one, Decl(inKeywordAndIntersection.ts, 14, 23)) | ||
>instance : Symbol(instance, Decl(inKeywordAndIntersection.ts, 22, 5)) | ||
>one : Symbol(InstanceOne.one, Decl(inKeywordAndIntersection.ts, 14, 23)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
=== tests/cases/compiler/inKeywordAndIntersection.ts === | ||
class A { a = 0 } | ||
>A : A | ||
>a : number | ||
>0 : 0 | ||
|
||
class B { b = 0 } | ||
>B : B | ||
>b : number | ||
>0 : 0 | ||
|
||
function f10(obj: A & { x: string } | B) { | ||
>f10 : (obj: (A & { x: string;}) | B) => void | ||
>obj : B | (A & { x: string; }) | ||
>x : string | ||
|
||
if (obj instanceof Object) { | ||
>obj instanceof Object : boolean | ||
>obj : B | (A & { x: string; }) | ||
>Object : ObjectConstructor | ||
|
||
obj; // A & { x: string } | B | ||
>obj : B | (A & { x: string; }) | ||
} | ||
else { | ||
obj; // Error | ||
>obj : never | ||
} | ||
} | ||
|
||
// Repro from #50844 | ||
|
||
interface InstanceOne { | ||
one(): void | ||
>one : () => void | ||
} | ||
|
||
interface InstanceTwo { | ||
two(): void | ||
>two : () => void | ||
} | ||
|
||
const instance = {} as InstanceOne | InstanceTwo | ||
>instance : InstanceOne | InstanceTwo | ||
>{} as InstanceOne | InstanceTwo : InstanceOne | InstanceTwo | ||
>{} : {} | ||
|
||
const ClassOne = {} as { new(): InstanceOne } & { foo: true }; | ||
>ClassOne : (new () => InstanceOne) & { foo: true; } | ||
>{} as { new(): InstanceOne } & { foo: true } : (new () => InstanceOne) & { foo: true; } | ||
>{} : {} | ||
>foo : true | ||
>true : true | ||
|
||
if (instance instanceof ClassOne) { | ||
>instance instanceof ClassOne : boolean | ||
>instance : InstanceOne | InstanceTwo | ||
>ClassOne : (new () => InstanceOne) & { foo: true; } | ||
|
||
instance.one(); | ||
>instance.one() : void | ||
>instance.one : () => void | ||
>instance : InstanceOne | ||
>one : () => void | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.