Skip to content

Separate intersection type resolution process for publicly and non-publicly accessible mixin construct signatures #45810

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10937,24 +10937,33 @@ namespace ts {
const types = type.types;
const mixinFlags = findMixins(types);
const mixinCount = countWhere(mixinFlags, (b) => b);
const allTypeSignatures = types.map((t) => getSignaturesOfType(t, SignatureKind.Construct));
// Flag mixins with only publicly accessible constructors
const accessibleMixinFlags = mixinFlags.map((flag, i) => flag && allTypeSignatures[i].every(({declaration}) => declaration && !getSelectedEffectiveModifierFlags(declaration, ModifierFlags.NonPublicAccessibilityModifier)));
for (let i = 0; i < types.length; i++) {
const t = type.types[i];
// When an intersection type contains mixin constructor types, the construct signatures from
// those types are discarded and their return types are mixed into the return types of all
// other construct signatures in the intersection type. For example, the intersection type
// '{ new(...args: any[]) => A } & { new(s: string) => B }' has a single construct signature
// 'new(s: string) => A & B'.
if (!mixinFlags[i]) {
let signatures = getSignaturesOfType(t, SignatureKind.Construct);
if (signatures.length && mixinCount > 0) {
signatures = map(signatures, s => {
const clone = cloneSignature(s);
clone.resolvedReturnType = includeMixinType(getReturnTypeOfSignature(s), types, mixinFlags, i);
return clone;
});
}
constructSignatures = appendSignatures(constructSignatures, signatures);
const t = types[i];
let signatures = allTypeSignatures[i];
// When an intersection type contains mixin constructor types...
if (mixinFlags[i]) {
// ...the NON-PUBLICLY ACCESSIBLE construct signatures (private, protected) from those mixin types
// are kept and added to the intersection type construct signatures
signatures = signatures.filter(({declaration}) => declaration && getSelectedEffectiveModifierFlags(declaration, ModifierFlags.NonPublicAccessibilityModifier));
}
else if (signatures.length && mixinCount > 0) {
// ...the PUBLICLY ACCESSIBLE construct signatures from those mixin types
// are discarded and their return types are mixed into the return types of all
// other construct signatures in the intersection type. For example, the intersection type
// '{ new(...args: any[]) => A } & { new(s: string) => B }' has a single construct signature
// 'new(s: string) => A & B'.
signatures = map(signatures, s => {
const clone = cloneSignature(s);
// accessibleMixinFlags is used here to prevent the return types of construct signatures of mixins with non-public constructors
// getting mixed into the intersection type's construct signatures.
clone.resolvedReturnType = includeMixinType(getReturnTypeOfSignature(s), types, accessibleMixinFlags, i);
return clone;
});
}
constructSignatures = appendSignatures(constructSignatures, signatures);
callSignatures = appendSignatures(callSignatures, getSignaturesOfType(t, SignatureKind.Call));
indexInfos = reduceLeft(getIndexInfosOfType(t), (infos, newInfo) => appendIndexInfo(infos, newInfo, /*union*/ false), indexInfos);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
tests/cases/conformance/classes/mixinClassesConstructorAccessibility.ts(53,3): error TS2673: Constructor of class 'PrivateConstructorMixin' is private and only accessible within the class declaration.
tests/cases/conformance/classes/mixinClassesConstructorAccessibility.ts(54,3): error TS2673: Constructor of class 'PrivateConstructorMixinGenericBaseType' is private and only accessible within the class declaration.
tests/cases/conformance/classes/mixinClassesConstructorAccessibility.ts(56,3): error TS2674: Constructor of class 'ProtectedConstructorMixin' is protected and only accessible within the class declaration.
tests/cases/conformance/classes/mixinClassesConstructorAccessibility.ts(57,3): error TS2674: Constructor of class 'ProtectedConstructorMixinGenericBaseType' is protected and only accessible within the class declaration.


==== tests/cases/conformance/classes/mixinClassesConstructorAccessibility.ts (4 errors) ====
function PrivateConstructorMixinGenericBaseType<TBase extends new(...args: any[]) => any>(Base: TBase) {
return class PrivateConstructorMixinGenericBaseType extends Base {
private constructor(...args: any[]) {
super();
}
}
}

function PrivateConstructorMixin(Base: new(...args: any[]) => any) {
return class PrivateConstructorMixin extends Base {
private constructor(...args: any[]) {
super();
}
}
}

function ProtectedConstructorMixinGenericBaseType<TBase extends new(...args: any[]) => any>(Base: TBase) {
return class ProtectedConstructorMixinGenericBaseType extends Base {
protected constructor(...args: any[]) {
super();
}
}
}

function ProtectedConstructorMixin(Base: new(...args: any[]) => any) {
return class ProtectedConstructorMixin extends Base {
protected constructor(...args: any[]) {
super();
}
}
}

function PublicConstructorMixinGenericBaseType(Base: new(...args: any[]) => any) {
return class PublicConstructorMixinGenericBaseType extends Base {
constructor(...args: any[]) {
super();
}
}
}

function PublicConstructorMixin(Base: new(...args: any[]) => any) {
return class PublicConstructorMixin extends Base {
constructor(...args: any[]) {
super();
}
}
}

class Base {
constructor() {}
}

new (PrivateConstructorMixin(Base))(); // error: PrivateConstructorMixin is private
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2673: Constructor of class 'PrivateConstructorMixin' is private and only accessible within the class declaration.
new (PrivateConstructorMixinGenericBaseType(Base))(); // error: PrivateConstructorMixinGenericBaseType is private
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2673: Constructor of class 'PrivateConstructorMixinGenericBaseType' is private and only accessible within the class declaration.

new (ProtectedConstructorMixin(Base))(); // error: ProtectedConstructorMixin is protected
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2674: Constructor of class 'ProtectedConstructorMixin' is protected and only accessible within the class declaration.
new (ProtectedConstructorMixinGenericBaseType(Base))(); // error: ProtectedConstructorMixinGenericBaseType is protected
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2674: Constructor of class 'ProtectedConstructorMixinGenericBaseType' is protected and only accessible within the class declaration.

new (PublicConstructorMixin(Base))();
new (PublicConstructorMixinGenericBaseType(Base))();
167 changes: 167 additions & 0 deletions tests/baselines/reference/mixinClassesConstructorAccessibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//// [mixinClassesConstructorAccessibility.ts]
function PrivateConstructorMixinGenericBaseType<TBase extends new(...args: any[]) => any>(Base: TBase) {
return class PrivateConstructorMixinGenericBaseType extends Base {
private constructor(...args: any[]) {
super();
}
}
}

function PrivateConstructorMixin(Base: new(...args: any[]) => any) {
return class PrivateConstructorMixin extends Base {
private constructor(...args: any[]) {
super();
}
}
}

function ProtectedConstructorMixinGenericBaseType<TBase extends new(...args: any[]) => any>(Base: TBase) {
return class ProtectedConstructorMixinGenericBaseType extends Base {
protected constructor(...args: any[]) {
super();
}
}
}

function ProtectedConstructorMixin(Base: new(...args: any[]) => any) {
return class ProtectedConstructorMixin extends Base {
protected constructor(...args: any[]) {
super();
}
}
}

function PublicConstructorMixinGenericBaseType(Base: new(...args: any[]) => any) {
return class PublicConstructorMixinGenericBaseType extends Base {
constructor(...args: any[]) {
super();
}
}
}

function PublicConstructorMixin(Base: new(...args: any[]) => any) {
return class PublicConstructorMixin extends Base {
constructor(...args: any[]) {
super();
}
}
}

class Base {
constructor() {}
}

new (PrivateConstructorMixin(Base))(); // error: PrivateConstructorMixin is private
new (PrivateConstructorMixinGenericBaseType(Base))(); // error: PrivateConstructorMixinGenericBaseType is private

new (ProtectedConstructorMixin(Base))(); // error: ProtectedConstructorMixin is protected
new (ProtectedConstructorMixinGenericBaseType(Base))(); // error: ProtectedConstructorMixinGenericBaseType is protected

new (PublicConstructorMixin(Base))();
new (PublicConstructorMixinGenericBaseType(Base))();

//// [mixinClassesConstructorAccessibility.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
function PrivateConstructorMixinGenericBaseType(Base) {
return /** @class */ (function (_super) {
__extends(PrivateConstructorMixinGenericBaseType, _super);
function PrivateConstructorMixinGenericBaseType() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return PrivateConstructorMixinGenericBaseType;
}(Base));
}
function PrivateConstructorMixin(Base) {
return /** @class */ (function (_super) {
__extends(PrivateConstructorMixin, _super);
function PrivateConstructorMixin() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return PrivateConstructorMixin;
}(Base));
}
function ProtectedConstructorMixinGenericBaseType(Base) {
return /** @class */ (function (_super) {
__extends(ProtectedConstructorMixinGenericBaseType, _super);
function ProtectedConstructorMixinGenericBaseType() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return ProtectedConstructorMixinGenericBaseType;
}(Base));
}
function ProtectedConstructorMixin(Base) {
return /** @class */ (function (_super) {
__extends(ProtectedConstructorMixin, _super);
function ProtectedConstructorMixin() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return ProtectedConstructorMixin;
}(Base));
}
function PublicConstructorMixinGenericBaseType(Base) {
return /** @class */ (function (_super) {
__extends(PublicConstructorMixinGenericBaseType, _super);
function PublicConstructorMixinGenericBaseType() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return PublicConstructorMixinGenericBaseType;
}(Base));
}
function PublicConstructorMixin(Base) {
return /** @class */ (function (_super) {
__extends(PublicConstructorMixin, _super);
function PublicConstructorMixin() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this) || this;
}
return PublicConstructorMixin;
}(Base));
}
var Base = /** @class */ (function () {
function Base() {
}
return Base;
}());
new (PrivateConstructorMixin(Base))(); // error: PrivateConstructorMixin is private
new (PrivateConstructorMixinGenericBaseType(Base))(); // error: PrivateConstructorMixinGenericBaseType is private
new (ProtectedConstructorMixin(Base))(); // error: ProtectedConstructorMixin is protected
new (ProtectedConstructorMixinGenericBaseType(Base))(); // error: ProtectedConstructorMixinGenericBaseType is protected
new (PublicConstructorMixin(Base))();
new (PublicConstructorMixinGenericBaseType(Base))();
Loading