-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix isTypeDerivedFrom to properly handle {}
and intersections
#51631
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: what does "less derived" mean? how something can be more or less derived?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means that any object-like type, including
Object
, appears to be derived from{}
. The effect of this addition is thatx instanceof Object
will narrow{}
toObject
, which in turn means thatx
qualifies as the right hand operand of anin
check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably reword this like this then:
Maybe I'm just missing the appropriate background and lingo but this explanation is more readable to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the context of type theory, "less derived" is definitely a thing. e.g.
Animal
is less derived thanDog
which is less derived thanPitBull
, whileCat
andDog
are equally derived, being sister types. These relationships are obvious under nominal typing, but probably get pretty hairy under structural typing.