-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Prevent merged class/namespace from overlapping with Record<string, unknown>
#47088
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
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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/mergedClassNamespaceRecordCast.errors.txt
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,34 @@ | ||
tests/cases/compiler/mergedClassNamespaceRecordCast.ts(3,1): error TS2352: Conversion of type 'C1' to type 'Record<string, unknown>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
Index signature for type 'string' is missing in type 'C1'. | ||
tests/cases/compiler/mergedClassNamespaceRecordCast.ts(9,1): error TS2352: Conversion of type 'C2' to type 'Record<string, unknown>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
Index signature for type 'string' is missing in type 'C2'. | ||
tests/cases/compiler/mergedClassNamespaceRecordCast.ts(12,10): error TS2339: Property 'unrelated' does not exist on type 'C2'. | ||
|
||
|
||
==== tests/cases/compiler/mergedClassNamespaceRecordCast.ts (3 errors) ==== | ||
class C1 { foo() {} } | ||
|
||
new C1() as Record<string, unknown>; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2352: Conversion of type 'C1' to type 'Record<string, unknown>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
!!! error TS2352: Index signature for type 'string' is missing in type 'C1'. | ||
|
||
|
||
class C2 { foo() {} } | ||
namespace C2 { export const unrelated = 3; } | ||
|
||
new C2() as Record<string, unknown>; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2352: Conversion of type 'C2' to type 'Record<string, unknown>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. | ||
!!! error TS2352: Index signature for type 'string' is missing in type 'C2'. | ||
|
||
C2.unrelated | ||
new C2().unrelated | ||
~~~~~~~~~ | ||
!!! error TS2339: Property 'unrelated' does not exist on type 'C2'. | ||
|
||
|
||
namespace C3 { export const unrelated = 3; } | ||
|
||
C3 as Record<string, unknown>; | ||
|
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/mergedClassNamespaceRecordCast.js
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,45 @@ | ||
//// [mergedClassNamespaceRecordCast.ts] | ||
class C1 { foo() {} } | ||
|
||
new C1() as Record<string, unknown>; | ||
|
||
|
||
class C2 { foo() {} } | ||
namespace C2 { export const unrelated = 3; } | ||
|
||
new C2() as Record<string, unknown>; | ||
|
||
C2.unrelated | ||
new C2().unrelated | ||
|
||
|
||
namespace C3 { export const unrelated = 3; } | ||
|
||
C3 as Record<string, unknown>; | ||
|
||
|
||
//// [mergedClassNamespaceRecordCast.js] | ||
var C1 = /** @class */ (function () { | ||
function C1() { | ||
} | ||
C1.prototype.foo = function () { }; | ||
return C1; | ||
}()); | ||
new C1(); | ||
var C2 = /** @class */ (function () { | ||
function C2() { | ||
} | ||
C2.prototype.foo = function () { }; | ||
return C2; | ||
}()); | ||
(function (C2) { | ||
C2.unrelated = 3; | ||
})(C2 || (C2 = {})); | ||
new C2(); | ||
C2.unrelated; | ||
new C2().unrelated; | ||
var C3; | ||
(function (C3) { | ||
C3.unrelated = 3; | ||
})(C3 || (C3 = {})); | ||
C3; |
39 changes: 39 additions & 0 deletions
39
tests/baselines/reference/mergedClassNamespaceRecordCast.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,39 @@ | ||
=== tests/cases/compiler/mergedClassNamespaceRecordCast.ts === | ||
class C1 { foo() {} } | ||
>C1 : Symbol(C1, Decl(mergedClassNamespaceRecordCast.ts, 0, 0)) | ||
>foo : Symbol(C1.foo, Decl(mergedClassNamespaceRecordCast.ts, 0, 10)) | ||
|
||
new C1() as Record<string, unknown>; | ||
>C1 : Symbol(C1, Decl(mergedClassNamespaceRecordCast.ts, 0, 0)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
|
||
class C2 { foo() {} } | ||
>C2 : Symbol(C2, Decl(mergedClassNamespaceRecordCast.ts, 2, 36), Decl(mergedClassNamespaceRecordCast.ts, 5, 21)) | ||
>foo : Symbol(C2.foo, Decl(mergedClassNamespaceRecordCast.ts, 5, 10)) | ||
|
||
namespace C2 { export const unrelated = 3; } | ||
>C2 : Symbol(C2, Decl(mergedClassNamespaceRecordCast.ts, 2, 36), Decl(mergedClassNamespaceRecordCast.ts, 5, 21)) | ||
>unrelated : Symbol(unrelated, Decl(mergedClassNamespaceRecordCast.ts, 6, 27)) | ||
|
||
new C2() as Record<string, unknown>; | ||
>C2 : Symbol(C2, Decl(mergedClassNamespaceRecordCast.ts, 2, 36), Decl(mergedClassNamespaceRecordCast.ts, 5, 21)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
C2.unrelated | ||
>C2.unrelated : Symbol(C2.unrelated, Decl(mergedClassNamespaceRecordCast.ts, 6, 27)) | ||
>C2 : Symbol(C2, Decl(mergedClassNamespaceRecordCast.ts, 2, 36), Decl(mergedClassNamespaceRecordCast.ts, 5, 21)) | ||
>unrelated : Symbol(C2.unrelated, Decl(mergedClassNamespaceRecordCast.ts, 6, 27)) | ||
|
||
new C2().unrelated | ||
>C2 : Symbol(C2, Decl(mergedClassNamespaceRecordCast.ts, 2, 36), Decl(mergedClassNamespaceRecordCast.ts, 5, 21)) | ||
|
||
|
||
namespace C3 { export const unrelated = 3; } | ||
>C3 : Symbol(C3, Decl(mergedClassNamespaceRecordCast.ts, 11, 18)) | ||
>unrelated : Symbol(unrelated, Decl(mergedClassNamespaceRecordCast.ts, 14, 27)) | ||
|
||
C3 as Record<string, unknown>; | ||
>C3 : Symbol(C3, Decl(mergedClassNamespaceRecordCast.ts, 11, 18)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/mergedClassNamespaceRecordCast.types
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,46 @@ | ||
=== tests/cases/compiler/mergedClassNamespaceRecordCast.ts === | ||
class C1 { foo() {} } | ||
>C1 : C1 | ||
>foo : () => void | ||
|
||
new C1() as Record<string, unknown>; | ||
>new C1() as Record<string, unknown> : Record<string, unknown> | ||
>new C1() : C1 | ||
>C1 : typeof C1 | ||
|
||
|
||
class C2 { foo() {} } | ||
>C2 : C2 | ||
>foo : () => void | ||
|
||
namespace C2 { export const unrelated = 3; } | ||
>C2 : typeof C2 | ||
>unrelated : 3 | ||
>3 : 3 | ||
|
||
new C2() as Record<string, unknown>; | ||
>new C2() as Record<string, unknown> : Record<string, unknown> | ||
>new C2() : C2 | ||
>C2 : typeof C2 | ||
|
||
C2.unrelated | ||
>C2.unrelated : 3 | ||
>C2 : typeof C2 | ||
>unrelated : 3 | ||
|
||
new C2().unrelated | ||
>new C2().unrelated : any | ||
>new C2() : C2 | ||
>C2 : typeof C2 | ||
>unrelated : any | ||
|
||
|
||
namespace C3 { export const unrelated = 3; } | ||
>C3 : typeof C3 | ||
>unrelated : 3 | ||
>3 : 3 | ||
|
||
C3 as Record<string, unknown>; | ||
>C3 as Record<string, unknown> : Record<string, unknown> | ||
>C3 : typeof C3 | ||
|
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,17 @@ | ||
class C1 { foo() {} } | ||
|
||
new C1() as Record<string, unknown>; | ||
|
||
|
||
class C2 { foo() {} } | ||
namespace C2 { export const unrelated = 3; } | ||
|
||
new C2() as Record<string, unknown>; | ||
|
||
C2.unrelated | ||
new C2().unrelated | ||
|
||
|
||
namespace C3 { export const unrelated = 3; } | ||
|
||
C3 as Record<string, unknown>; |
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.
Is there a reason not to just use
!(type.symbol.flags & SymbolFlags.Class)
(dropping the “and is also a value module”)? It looks to me like we believe namespaces generally have an inferable index, and classes don’t, and we want the classiness property to trump the namespacey property in this calculation. It doesn’t seem to need the specificity of “classes merged with namespaces.”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.
Probably not; things still work as expected with that simplification. I just took the solution suggested in the issue and thumbs-up'd by Ryan.
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.
Switched it over.