-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Constructor Visibility #6885
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
Constructor Visibility #6885
Changes from 1 commit
f869b41
c351ffc
bbf92ce
3d529a8
4200051
4c93eb2
5ce0202
16b54e0
037b657
a6a5a22
d0843e1
8ef9599
ec7e80e
91ea83c
208830e
2d7a0f4
ba8b168
de23e2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1711,6 +1711,16 @@ namespace ts { | |
| return result; | ||
| } | ||
|
|
||
| function visibilityToString(flags: NodeFlags) { | ||
| if (flags === NodeFlags.Private) { | ||
| return "private"; | ||
| } | ||
| if (flags === NodeFlags.Protected) { | ||
| return "protected"; | ||
| } | ||
| return "public"; | ||
| } | ||
|
|
||
| function getTypeAliasForTypeLiteral(type: Type): Symbol { | ||
| if (type.symbol && type.symbol.flags & SymbolFlags.TypeLiteral) { | ||
| let node = type.symbol.declarations[0].parent; | ||
|
|
@@ -6059,28 +6069,24 @@ namespace ts { | |
|
|
||
| function constructorRelatedTo(sourceSignature: Signature, targetSignature: Signature, reportErrors: boolean) { | ||
| if (sourceSignature && targetSignature && sourceSignature.declaration && targetSignature.declaration) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are cases where the declarations are |
||
| // A public, protected and private signature is assignable to a private signature. | ||
| // A public and protected signature is assignable to a protected signature. | ||
| // And only a public signature is assignable to public signature. | ||
| const sourceAccessibility = sourceSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); | ||
| const targetAccessibility = targetSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); | ||
|
|
||
| const isRelated = sourceAccessibility === targetAccessibility; | ||
| const isRelated = targetAccessibility === NodeFlags.Private | ||
| || (targetAccessibility === NodeFlags.Protected && sourceAccessibility !== NodeFlags.Private) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indent these
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd just be clearer if you formatted this as // A public, protected and private signature is assignable to a private signature.
if (targetAccessibility === NodeFlags.Private) {
return true;
}
// A public and protected signature is assignable to a protected signature.
if (targetAccessibility === NodeFlags.Protected && sourceAccessibility !== NodeFlags.Private) {
return true;
}
// Only a public signature is assignable to public signature.
if (targetAccessibility !== NodeFlags.Protected && !sourceAccessibility) {
return true;
}
if (reportErrors) {
reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility));
}
return false;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Done |
||
| || (targetAccessibility !== NodeFlags.Protected && !(sourceAccessibility & (NodeFlags.Private | NodeFlags.Protected))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, my bad! |
||
|
|
||
| if (!isRelated && reportErrors) { | ||
| reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, flagsToString(sourceAccessibility), flagsToString(targetAccessibility)); | ||
| reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); | ||
| } | ||
|
|
||
| return isRelated; | ||
| } | ||
|
|
||
| return true; | ||
|
|
||
| function flagsToString(flags: NodeFlags) { | ||
| if (flags === NodeFlags.Private) { | ||
| return "private"; | ||
| } | ||
| if (flags === NodeFlags.Protected) { | ||
| return "protected"; | ||
| } | ||
| return "public"; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't get rid of the |
||
| } | ||
|
|
||
|
|
||
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 looks like a better name would be
constructorVisibilitiesAreCompatibleThere 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.
Done