From f869b41f144be3cbed1aa0ad3c66e125d57f9752 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Wed, 3 Feb 2016 01:07:09 +0000 Subject: [PATCH 01/55] Added private and protected modifiers to constructors --- src/compiler/checker.ts | 101 ++++++++++++++++++++++----- src/compiler/diagnosticMessages.json | 16 +++++ 2 files changed, 101 insertions(+), 16 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 07ec5023d5b..1ce5b51c66d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5889,16 +5889,20 @@ namespace ts { const sourceSignatures = getSignaturesOfType(source, kind); const targetSignatures = getSignaturesOfType(target, kind); - if (kind === SignatureKind.Construct && sourceSignatures.length && targetSignatures.length && - isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { - // An abstract constructor type is not assignable to a non-abstract constructor type - // as it would otherwise be possible to new an abstract class. Note that the assignablity - // check we perform for an extends clause excludes construct signatures from the target, - // so this check never proceeds. - if (reportErrors) { - reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); + if (kind === SignatureKind.Construct && sourceSignatures.length && targetSignatures.length) { + if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { + // An abstract constructor type is not assignable to a non-abstract constructor type + // as it would otherwise be possible to new an abstract class. Note that the assignablity + // check we perform for an extends clause excludes construct signatures from the target, + // so this check never proceeds. + if (reportErrors) { + reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); + } + return Ternary.False; + } + if (!constructorRelatedTo(sourceSignatures[0], targetSignatures[0], reportErrors)) { + return Ternary.False; } - return Ternary.False; } let result = Ternary.True; @@ -6052,6 +6056,32 @@ namespace ts { } return Ternary.True; } + + function constructorRelatedTo(sourceSignature: Signature, targetSignature: Signature, reportErrors: boolean) { + if (sourceSignature && targetSignature && sourceSignature.declaration && targetSignature.declaration) { + const sourceAccessibility = sourceSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); + const targetAccessibility = targetSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); + + const isRelated = sourceAccessibility === targetAccessibility; + if (!isRelated && reportErrors) { + reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, flagsToString(sourceAccessibility), flagsToString(targetAccessibility)); + } + + return isRelated; + } + + return true; + + function flagsToString(flags: NodeFlags) { + if (flags === NodeFlags.Private) { + return "private"; + } + if (flags === NodeFlags.Protected) { + return "protected"; + } + return "public"; + } + } } // Return true if the given type is the constructor type for an abstract class @@ -10103,6 +10133,9 @@ namespace ts { // that the user will not add any. const constructSignatures = getSignaturesOfType(expressionType, SignatureKind.Construct); if (constructSignatures.length) { + if (!isConstructorAccessible(node, constructSignatures[0])) { + return resolveErrorCall(node); + } return resolveCall(node, constructSignatures, candidatesOutArray); } @@ -10123,6 +10156,37 @@ namespace ts { return resolveErrorCall(node); } + function isConstructorAccessible(node: NewExpression, signature: Signature) { + if (!signature || !signature.declaration) { + return true; + } + + const declaration = signature.declaration; + const flags = declaration.flags; + + // Public constructor is accessible. + if (!(flags & (NodeFlags.Private | NodeFlags.Protected))) { + return true; + } + + const declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); + const enclosingClassDeclaration = getContainingClass(node); + const enclosingClass = enclosingClassDeclaration ? getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassDeclaration)) : undefined; + + // A private or protected constructor can only be instantiated within it's own class + if (declaringClass !== enclosingClass) { + if (flags & NodeFlags.Private) { + error(node, Diagnostics.Constructor_of_type_0_is_private_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); + } + if (flags & NodeFlags.Protected) { + error(node, Diagnostics.Constructor_of_type_0_is_protected_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); + } + return false; + } + + return true; + } + function resolveTaggedTemplateExpression(node: TaggedTemplateExpression, candidatesOutArray: Signature[]): Signature { const tagType = checkExpression(node.tag); const apparentType = getApparentType(tagType); @@ -12059,7 +12123,7 @@ namespace ts { error(o.name, Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); } else if (deviation & (NodeFlags.Private | NodeFlags.Protected)) { - error(o.name, Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); + error(o.name || o, Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); } else if (deviation & NodeFlags.Abstract) { error(o.name, Diagnostics.Overload_signatures_must_all_be_abstract_or_not_abstract); @@ -13928,6 +13992,7 @@ namespace ts { if (baseTypes.length && produceDiagnostics) { const baseType = baseTypes[0]; const staticBaseType = getBaseConstructorTypeOfClass(type); + checkBaseTypeAccessibility(staticBaseType, baseTypeNode); checkSourceElement(baseTypeNode.expression); if (baseTypeNode.typeArguments) { forEach(baseTypeNode.typeArguments, checkSourceElement); @@ -13983,6 +14048,16 @@ namespace ts { } } + function checkBaseTypeAccessibility(type: ObjectType, node: ExpressionWithTypeArguments) { + const signatures = getSignaturesOfType(type, SignatureKind.Construct); + if (signatures.length) { + const declaration = signatures[0].declaration; + if (declaration && declaration.flags & NodeFlags.Private) { + error(node, Diagnostics.Cannot_extend_private_class_0, (node.expression).text); + } + } + } + function getTargetSymbol(s: Symbol) { // if symbol is instantiated its flags are not copied from the 'target' // so we'll need to get back original 'target' symbol to work with correct set of flags @@ -16348,12 +16423,6 @@ namespace ts { if (flags & NodeFlags.Abstract) { return grammarErrorOnNode(lastStatic, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); } - else if (flags & NodeFlags.Protected) { - return grammarErrorOnNode(lastProtected, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "protected"); - } - else if (flags & NodeFlags.Private) { - return grammarErrorOnNode(lastPrivate, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "private"); - } else if (flags & NodeFlags.Async) { return grammarErrorOnNode(lastAsync, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index c848ebc3a10..2ee388ff42e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1823,6 +1823,22 @@ "category": "Error", "code": 2671 }, + "Cannot assign a '{0}' constructor type to a '{1}' constructor type.": { + "category": "Error", + "code": 2672 + }, + "Constructor of type '{0}' is private and only accessible within class '{1}'.": { + "category": "Error", + "code": 2673 + }, + "Constructor of type '{0}' is protected and only accessible within class '{1}'.": { + "category": "Error", + "code": 2674 + }, + "Cannot extend private class '{0}'.": { + "category": "Error", + "code": 2675 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", "code": 4000 From c351ffcc75c1d257f72a3e86faaa640d8daefdf3 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Wed, 3 Feb 2016 22:48:33 +0000 Subject: [PATCH 02/55] Added and updated tests for constructor visibility --- .../baselines/reference/Protected3.errors.txt | 9 -- tests/baselines/reference/Protected3.symbols | 6 + tests/baselines/reference/Protected3.types | 6 + .../classConstructorAccessibility.errors.txt | 41 ++--- .../classConstructorAccessibility.js | 53 +++++-- .../classConstructorAccessibility2.errors.txt | 59 +++++++ .../classConstructorAccessibility2.js | 148 ++++++++++++++++++ .../classConstructorAccessibility3.errors.txt | 77 +++++++++ .../classConstructorAccessibility3.js | 98 ++++++++++++ ...nstructorOverloadsAccessibility.errors.txt | 45 ++++++ .../classConstructorOverloadsAccessibility.js | 76 +++++++++ ...implicitAnyInAmbientDeclaration.errors.txt | 5 +- ...licitAnyInAmbientDeclaration2.d.errors.txt | 5 +- .../parserConstructorDeclaration5.errors.txt | 9 -- .../parserConstructorDeclaration5.symbols | 6 + .../parserConstructorDeclaration5.types | 6 + .../reference/protectedMembers.errors.txt | 12 +- tests/baselines/reference/protectedMembers.js | 2 - .../typesWithPrivateConstructor.errors.txt | 28 +--- .../reference/typesWithPrivateConstructor.js | 23 ++- .../typesWithProtectedConstructor.errors.txt | 24 +++ .../typesWithProtectedConstructor.js | 45 ++++++ tests/cases/compiler/protectedMembers.ts | 1 - .../classConstructorAccessibility.ts | 18 ++- .../classConstructorAccessibility2.ts | 42 +++++ .../classConstructorAccessibility3.ts | 35 +++++ .../classConstructorOverloadsAccessibility.ts | 34 ++++ .../members/typesWithPrivateConstructor.ts | 6 +- .../members/typesWithProtectedConstructor.ts | 16 ++ .../quickInfoOnPrivateConstructorCall.ts | 9 ++ .../quickInfoOnProtectedConstructorCall.ts | 9 ++ 31 files changed, 843 insertions(+), 110 deletions(-) delete mode 100644 tests/baselines/reference/Protected3.errors.txt create mode 100644 tests/baselines/reference/Protected3.symbols create mode 100644 tests/baselines/reference/Protected3.types create mode 100644 tests/baselines/reference/classConstructorAccessibility2.errors.txt create mode 100644 tests/baselines/reference/classConstructorAccessibility2.js create mode 100644 tests/baselines/reference/classConstructorAccessibility3.errors.txt create mode 100644 tests/baselines/reference/classConstructorAccessibility3.js create mode 100644 tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt create mode 100644 tests/baselines/reference/classConstructorOverloadsAccessibility.js delete mode 100644 tests/baselines/reference/parserConstructorDeclaration5.errors.txt create mode 100644 tests/baselines/reference/parserConstructorDeclaration5.symbols create mode 100644 tests/baselines/reference/parserConstructorDeclaration5.types create mode 100644 tests/baselines/reference/typesWithProtectedConstructor.errors.txt create mode 100644 tests/baselines/reference/typesWithProtectedConstructor.js create mode 100644 tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts create mode 100644 tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts create mode 100644 tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts create mode 100644 tests/cases/conformance/types/members/typesWithProtectedConstructor.ts create mode 100644 tests/cases/fourslash/quickInfoOnPrivateConstructorCall.ts create mode 100644 tests/cases/fourslash/quickInfoOnProtectedConstructorCall.ts diff --git a/tests/baselines/reference/Protected3.errors.txt b/tests/baselines/reference/Protected3.errors.txt deleted file mode 100644 index 688422a1e0f..00000000000 --- a/tests/baselines/reference/Protected3.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts(2,3): error TS1089: 'protected' modifier cannot appear on a constructor declaration. - - -==== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts (1 errors) ==== - class C { - protected constructor() { } - ~~~~~~~~~ -!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration. - } \ No newline at end of file diff --git a/tests/baselines/reference/Protected3.symbols b/tests/baselines/reference/Protected3.symbols new file mode 100644 index 00000000000..a9882189902 --- /dev/null +++ b/tests/baselines/reference/Protected3.symbols @@ -0,0 +1,6 @@ +=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts === +class C { +>C : Symbol(C, Decl(Protected3.ts, 0, 0)) + + protected constructor() { } +} diff --git a/tests/baselines/reference/Protected3.types b/tests/baselines/reference/Protected3.types new file mode 100644 index 00000000000..d3f938a4431 --- /dev/null +++ b/tests/baselines/reference/Protected3.types @@ -0,0 +1,6 @@ +=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts === +class C { +>C : C + + protected constructor() { } +} diff --git a/tests/baselines/reference/classConstructorAccessibility.errors.txt b/tests/baselines/reference/classConstructorAccessibility.errors.txt index c22f3593bc5..a24ec6c63d1 100644 --- a/tests/baselines/reference/classConstructorAccessibility.errors.txt +++ b/tests/baselines/reference/classConstructorAccessibility.errors.txt @@ -1,29 +1,30 @@ -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(6,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(10,5): error TS1089: 'protected' modifier cannot appear on a constructor declaration. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(23,9): error TS1089: 'private' modifier cannot appear on a constructor declaration. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(27,9): error TS1089: 'protected' modifier cannot appear on a constructor declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(15,9): error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(16,9): error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(32,13): error TS2673: Constructor of type '(x: T): D' is private and only accessible within class 'D'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(33,13): error TS2674: Constructor of type '(x: T): E' is protected and only accessible within class 'E'. ==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ==== + class C { public constructor(public x: number) { } } class D { - private constructor(public x: number) { } // error - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. + private constructor(public x: number) { } } class E { - protected constructor(public x: number) { } // error - ~~~~~~~~~ -!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration. + protected constructor(public x: number) { } } var c = new C(1); - var d = new D(1); - var e = new E(1); + var d = new D(1); // error + ~~~~~~~~ +!!! error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'. + var e = new E(1); // error + ~~~~~~~~ +!!! error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'. module Generic { class C { @@ -31,19 +32,19 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib } class D { - private constructor(public x: T) { } // error - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. + private constructor(public x: T) { } } class E { - protected constructor(public x: T) { } // error - ~~~~~~~~~ -!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration. + protected constructor(public x: T) { } } var c = new C(1); - var d = new D(1); - var e = new E(1); + var d = new D(1); // error + ~~~~~~~~ +!!! error TS2673: Constructor of type '(x: T): D' is private and only accessible within class 'D'. + var e = new E(1); // error + ~~~~~~~~ +!!! error TS2674: Constructor of type '(x: T): E' is protected and only accessible within class 'E'. } \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility.js b/tests/baselines/reference/classConstructorAccessibility.js index fed96385845..de7e4a83bb8 100644 --- a/tests/baselines/reference/classConstructorAccessibility.js +++ b/tests/baselines/reference/classConstructorAccessibility.js @@ -1,19 +1,20 @@ //// [classConstructorAccessibility.ts] + class C { public constructor(public x: number) { } } class D { - private constructor(public x: number) { } // error + private constructor(public x: number) { } } class E { - protected constructor(public x: number) { } // error + protected constructor(public x: number) { } } var c = new C(1); -var d = new D(1); -var e = new E(1); +var d = new D(1); // error +var e = new E(1); // error module Generic { class C { @@ -21,16 +22,16 @@ module Generic { } class D { - private constructor(public x: T) { } // error + private constructor(public x: T) { } } class E { - protected constructor(public x: T) { } // error + protected constructor(public x: T) { } } var c = new C(1); - var d = new D(1); - var e = new E(1); + var d = new D(1); // error + var e = new E(1); // error } @@ -44,18 +45,18 @@ var C = (function () { var D = (function () { function D(x) { this.x = x; - } // error + } return D; }()); var E = (function () { function E(x) { this.x = x; - } // error + } return E; }()); var c = new C(1); -var d = new D(1); -var e = new E(1); +var d = new D(1); // error +var e = new E(1); // error var Generic; (function (Generic) { var C = (function () { @@ -67,16 +68,36 @@ var Generic; var D = (function () { function D(x) { this.x = x; - } // error + } return D; }()); var E = (function () { function E(x) { this.x = x; - } // error + } return E; }()); var c = new C(1); - var d = new D(1); - var e = new E(1); + var d = new D(1); // error + var e = new E(1); // error })(Generic || (Generic = {})); + + +//// [classConstructorAccessibility.d.ts] +declare class C { + x: number; + constructor(x: number); +} +declare class D { + x: number; + constructor(x); +} +declare class E { + x: number; + constructor(x: number); +} +declare var c: C; +declare var d: any; +declare var e: any; +declare module Generic { +} diff --git a/tests/baselines/reference/classConstructorAccessibility2.errors.txt b/tests/baselines/reference/classConstructorAccessibility2.errors.txt new file mode 100644 index 00000000000..1c6fc46edb3 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility2.errors.txt @@ -0,0 +1,59 @@ +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(26,28): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend private class 'BaseC'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,28): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,10): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(37,10): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. + + +==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ==== + + class BaseA { + public constructor(public x: number) { } + createInstance() { new BaseA(1); } + } + + class BaseB { + protected constructor(public x: number) { } + createInstance() { new BaseB(1); } + } + + class BaseC { + private constructor(public x: number) { } + createInstance() { new BaseC(1); } + } + + class DerivedA extends BaseA { + constructor(public x: number) { super(x); } + createInstance() { new DerivedA(1); } + createBaseInstance() { new BaseA(1); } + } + + class DerivedB extends BaseB { + constructor(public x: number) { super(x); } + createInstance() { new DerivedB(1); } + createBaseInstance() { new BaseB(1); } // error + ~~~~~~~~~~~~ +!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. + } + + class DerivedC extends BaseC { // error + ~~~~~ +!!! error TS2675: Cannot extend private class 'BaseC'. + constructor(public x: number) { super(x); } + createInstance() { new DerivedC(1); } + createBaseInstance() { new BaseC(1); } // error + ~~~~~~~~~~~~ +!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. + } + + var ba = new BaseA(1); + var bb = new BaseB(1); // error + ~~~~~~~~~~~~ +!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. + var bc = new BaseC(1); // error + ~~~~~~~~~~~~ +!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. + + var da = new DerivedA(1); + var db = new DerivedB(1); + var dc = new DerivedC(1); \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility2.js b/tests/baselines/reference/classConstructorAccessibility2.js new file mode 100644 index 00000000000..47c0b0bc626 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility2.js @@ -0,0 +1,148 @@ +//// [classConstructorAccessibility2.ts] + +class BaseA { + public constructor(public x: number) { } + createInstance() { new BaseA(1); } +} + +class BaseB { + protected constructor(public x: number) { } + createInstance() { new BaseB(1); } +} + +class BaseC { + private constructor(public x: number) { } + createInstance() { new BaseC(1); } +} + +class DerivedA extends BaseA { + constructor(public x: number) { super(x); } + createInstance() { new DerivedA(1); } + createBaseInstance() { new BaseA(1); } +} + +class DerivedB extends BaseB { + constructor(public x: number) { super(x); } + createInstance() { new DerivedB(1); } + createBaseInstance() { new BaseB(1); } // error +} + +class DerivedC extends BaseC { // error + constructor(public x: number) { super(x); } + createInstance() { new DerivedC(1); } + createBaseInstance() { new BaseC(1); } // error +} + +var ba = new BaseA(1); +var bb = new BaseB(1); // error +var bc = new BaseC(1); // error + +var da = new DerivedA(1); +var db = new DerivedB(1); +var dc = new DerivedC(1); + +//// [classConstructorAccessibility2.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var BaseA = (function () { + function BaseA(x) { + this.x = x; + } + BaseA.prototype.createInstance = function () { new BaseA(1); }; + return BaseA; +}()); +var BaseB = (function () { + function BaseB(x) { + this.x = x; + } + BaseB.prototype.createInstance = function () { new BaseB(1); }; + return BaseB; +}()); +var BaseC = (function () { + function BaseC(x) { + this.x = x; + } + BaseC.prototype.createInstance = function () { new BaseC(1); }; + return BaseC; +}()); +var DerivedA = (function (_super) { + __extends(DerivedA, _super); + function DerivedA(x) { + _super.call(this, x); + this.x = x; + } + DerivedA.prototype.createInstance = function () { new DerivedA(1); }; + DerivedA.prototype.createBaseInstance = function () { new BaseA(1); }; + return DerivedA; +}(BaseA)); +var DerivedB = (function (_super) { + __extends(DerivedB, _super); + function DerivedB(x) { + _super.call(this, x); + this.x = x; + } + DerivedB.prototype.createInstance = function () { new DerivedB(1); }; + DerivedB.prototype.createBaseInstance = function () { new BaseB(1); }; // error + return DerivedB; +}(BaseB)); +var DerivedC = (function (_super) { + __extends(DerivedC, _super); + function DerivedC(x) { + _super.call(this, x); + this.x = x; + } + DerivedC.prototype.createInstance = function () { new DerivedC(1); }; + DerivedC.prototype.createBaseInstance = function () { new BaseC(1); }; // error + return DerivedC; +}(BaseC)); +var ba = new BaseA(1); +var bb = new BaseB(1); // error +var bc = new BaseC(1); // error +var da = new DerivedA(1); +var db = new DerivedB(1); +var dc = new DerivedC(1); + + +//// [classConstructorAccessibility2.d.ts] +declare class BaseA { + x: number; + constructor(x: number); + createInstance(): void; +} +declare class BaseB { + x: number; + constructor(x: number); + createInstance(): void; +} +declare class BaseC { + x: number; + constructor(x); + createInstance(): void; +} +declare class DerivedA extends BaseA { + x: number; + constructor(x: number); + createInstance(): void; + createBaseInstance(): void; +} +declare class DerivedB extends BaseB { + x: number; + constructor(x: number); + createInstance(): void; + createBaseInstance(): void; +} +declare class DerivedC extends BaseC { + x: number; + constructor(x: number); + createInstance(): void; + createBaseInstance(): void; +} +declare var ba: BaseA; +declare var bb: any; +declare var bc: any; +declare var da: DerivedA; +declare var db: DerivedB; +declare var dc: DerivedC; diff --git a/tests/baselines/reference/classConstructorAccessibility3.errors.txt b/tests/baselines/reference/classConstructorAccessibility3.errors.txt new file mode 100644 index 00000000000..c536db1f9da --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility3.errors.txt @@ -0,0 +1,77 @@ +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(21,1): error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Foo'. + Cannot assign a 'protected' constructor type to a 'public' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(22,1): error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Foo'. + Cannot assign a 'private' constructor type to a 'public' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(26,1): error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Baz'. + Cannot assign a 'public' constructor type to a 'protected' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(27,1): error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Baz'. + Cannot assign a 'public' constructor type to a 'protected' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(28,1): error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Baz'. + Cannot assign a 'private' constructor type to a 'protected' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(32,1): error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Qux'. + Cannot assign a 'public' constructor type to a 'private' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(33,1): error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Qux'. + Cannot assign a 'public' constructor type to a 'private' constructor type. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(34,1): error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Qux'. + Cannot assign a 'protected' constructor type to a 'private' constructor type. + + +==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts (8 errors) ==== + + class Foo { + constructor(public x: number) { } + } + + class Bar { + public constructor(public x: number) { } + } + + class Baz { + protected constructor(public x: number) { } + } + + class Qux { + private constructor(public x: number) { } + } + + // b is public + let a = Foo; + a = Bar; + a = Baz; // error Baz is protected + ~ +!!! error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Foo'. +!!! error TS2322: Cannot assign a 'protected' constructor type to a 'public' constructor type. + a = Qux; // error Qux is private + ~ +!!! error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Foo'. +!!! error TS2322: Cannot assign a 'private' constructor type to a 'public' constructor type. + + // b is protected + let b = Baz; + b = Foo; // error Foo is public + ~ +!!! error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Baz'. +!!! error TS2322: Cannot assign a 'public' constructor type to a 'protected' constructor type. + b = Bar; // error Baz is public + ~ +!!! error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Baz'. +!!! error TS2322: Cannot assign a 'public' constructor type to a 'protected' constructor type. + b = Qux; // error Qux is private + ~ +!!! error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Baz'. +!!! error TS2322: Cannot assign a 'private' constructor type to a 'protected' constructor type. + + // c is private + let c = Qux; + c = Foo; // error Foo is public + ~ +!!! error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Qux'. +!!! error TS2322: Cannot assign a 'public' constructor type to a 'private' constructor type. + c = Bar; // error Bar is public + ~ +!!! error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Qux'. +!!! error TS2322: Cannot assign a 'public' constructor type to a 'private' constructor type. + c = Baz; // error Baz is protected + ~ +!!! error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Qux'. +!!! error TS2322: Cannot assign a 'protected' constructor type to a 'private' constructor type. \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility3.js b/tests/baselines/reference/classConstructorAccessibility3.js new file mode 100644 index 00000000000..8e900b6eb2d --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility3.js @@ -0,0 +1,98 @@ +//// [classConstructorAccessibility3.ts] + +class Foo { + constructor(public x: number) { } +} + +class Bar { + public constructor(public x: number) { } +} + +class Baz { + protected constructor(public x: number) { } +} + +class Qux { + private constructor(public x: number) { } +} + +// b is public +let a = Foo; +a = Bar; +a = Baz; // error Baz is protected +a = Qux; // error Qux is private + +// b is protected +let b = Baz; +b = Foo; // error Foo is public +b = Bar; // error Baz is public +b = Qux; // error Qux is private + +// c is private +let c = Qux; +c = Foo; // error Foo is public +c = Bar; // error Bar is public +c = Baz; // error Baz is protected + +//// [classConstructorAccessibility3.js] +var Foo = (function () { + function Foo(x) { + this.x = x; + } + return Foo; +}()); +var Bar = (function () { + function Bar(x) { + this.x = x; + } + return Bar; +}()); +var Baz = (function () { + function Baz(x) { + this.x = x; + } + return Baz; +}()); +var Qux = (function () { + function Qux(x) { + this.x = x; + } + return Qux; +}()); +// b is public +var a = Foo; +a = Bar; +a = Baz; // error Baz is protected +a = Qux; // error Qux is private +// b is protected +var b = Baz; +b = Foo; // error Foo is public +b = Bar; // error Baz is public +b = Qux; // error Qux is private +// c is private +var c = Qux; +c = Foo; // error Foo is public +c = Bar; // error Bar is public +c = Baz; // error Baz is protected + + +//// [classConstructorAccessibility3.d.ts] +declare class Foo { + x: number; + constructor(x: number); +} +declare class Bar { + x: number; + constructor(x: number); +} +declare class Baz { + x: number; + constructor(x: number); +} +declare class Qux { + x: number; + constructor(x); +} +declare let a: typeof Foo; +declare let b: typeof Baz; +declare let c: typeof Qux; diff --git a/tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt b/tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt new file mode 100644 index 00000000000..8882b9f0180 --- /dev/null +++ b/tests/baselines/reference/classConstructorOverloadsAccessibility.errors.txt @@ -0,0 +1,45 @@ +tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts(3,2): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts(4,2): error TS2385: Overload signatures must all be public, private or protected. +tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts(12,2): error TS2385: Overload signatures must all be public, private or protected. + + +==== tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts (3 errors) ==== + + class A { + public constructor(a: boolean) // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + protected constructor(a: number) // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + private constructor(a: string) + private constructor() { + + } + } + + class B { + protected constructor(a: number) // error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2385: Overload signatures must all be public, private or protected. + constructor(a: string) + constructor() { + + } + } + + class C { + protected constructor(a: number) + protected constructor(a: string) + protected constructor() { + + } + } + + class D { + constructor(a: number) + constructor(a: string) + public constructor() { + + } + } \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorOverloadsAccessibility.js b/tests/baselines/reference/classConstructorOverloadsAccessibility.js new file mode 100644 index 00000000000..b69257a1c2a --- /dev/null +++ b/tests/baselines/reference/classConstructorOverloadsAccessibility.js @@ -0,0 +1,76 @@ +//// [classConstructorOverloadsAccessibility.ts] + +class A { + public constructor(a: boolean) // error + protected constructor(a: number) // error + private constructor(a: string) + private constructor() { + + } +} + +class B { + protected constructor(a: number) // error + constructor(a: string) + constructor() { + + } +} + +class C { + protected constructor(a: number) + protected constructor(a: string) + protected constructor() { + + } +} + +class D { + constructor(a: number) + constructor(a: string) + public constructor() { + + } +} + +//// [classConstructorOverloadsAccessibility.js] +var A = (function () { + function A() { + } + return A; +}()); +var B = (function () { + function B() { + } + return B; +}()); +var C = (function () { + function C() { + } + return C; +}()); +var D = (function () { + function D() { + } + return D; +}()); + + +//// [classConstructorOverloadsAccessibility.d.ts] +declare class A { + constructor(a: boolean); + constructor(a: number); + constructor(a); +} +declare class B { + constructor(a: number); + constructor(a: string); +} +declare class C { + constructor(a: number); + constructor(a: string); +} +declare class D { + constructor(a: number); + constructor(a: string); +} diff --git a/tests/baselines/reference/implicitAnyInAmbientDeclaration.errors.txt b/tests/baselines/reference/implicitAnyInAmbientDeclaration.errors.txt index 1e66f26d0d2..493fc13db04 100644 --- a/tests/baselines/reference/implicitAnyInAmbientDeclaration.errors.txt +++ b/tests/baselines/reference/implicitAnyInAmbientDeclaration.errors.txt @@ -1,10 +1,9 @@ tests/cases/compiler/implicitAnyInAmbientDeclaration.ts(3,9): error TS7008: Member 'publicMember' implicitly has an 'any' type. tests/cases/compiler/implicitAnyInAmbientDeclaration.ts(6,16): error TS7010: 'publicFunction', which lacks return-type annotation, implicitly has an 'any' return type. tests/cases/compiler/implicitAnyInAmbientDeclaration.ts(6,31): error TS7006: Parameter 'x' implicitly has an 'any' type. -tests/cases/compiler/implicitAnyInAmbientDeclaration.ts(8,9): error TS1089: 'private' modifier cannot appear on a constructor declaration. -==== tests/cases/compiler/implicitAnyInAmbientDeclaration.ts (4 errors) ==== +==== tests/cases/compiler/implicitAnyInAmbientDeclaration.ts (3 errors) ==== module Test { declare class C { public publicMember; // this should be an error @@ -19,7 +18,5 @@ tests/cases/compiler/implicitAnyInAmbientDeclaration.ts(8,9): error TS1089: 'pri !!! error TS7006: Parameter 'x' implicitly has an 'any' type. private privateFunction(privateParam); // this should not be an error private constructor(privateParam); - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. } } \ No newline at end of file diff --git a/tests/baselines/reference/implicitAnyInAmbientDeclaration2.d.errors.txt b/tests/baselines/reference/implicitAnyInAmbientDeclaration2.d.errors.txt index 54fe39e2efe..03de82c731d 100644 --- a/tests/baselines/reference/implicitAnyInAmbientDeclaration2.d.errors.txt +++ b/tests/baselines/reference/implicitAnyInAmbientDeclaration2.d.errors.txt @@ -4,11 +4,10 @@ tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(2,13): error TS7005: tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(4,5): error TS7008: Member 'publicMember' implicitly has an 'any' type. tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(7,12): error TS7010: 'publicFunction', which lacks return-type annotation, implicitly has an 'any' return type. tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(7,27): error TS7006: Parameter 'x' implicitly has an 'any' type. -tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(9,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(13,24): error TS7006: Parameter 'publicConsParam' implicitly has an 'any' type. -==== tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts (8 errors) ==== +==== tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts (7 errors) ==== declare function foo(x); // this should be an error ~~~ !!! error TS7010: 'foo', which lacks return-type annotation, implicitly has an 'any' return type. @@ -30,8 +29,6 @@ tests/cases/compiler/implicitAnyInAmbientDeclaration2.d.ts(13,24): error TS7006: !!! error TS7006: Parameter 'x' implicitly has an 'any' type. private privateFunction(privateParam); // this should not be an error private constructor(privateParam); // this should not be an error - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. } declare class D { diff --git a/tests/baselines/reference/parserConstructorDeclaration5.errors.txt b/tests/baselines/reference/parserConstructorDeclaration5.errors.txt deleted file mode 100644 index 4589ad95a32..00000000000 --- a/tests/baselines/reference/parserConstructorDeclaration5.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration5.ts(2,3): error TS1089: 'private' modifier cannot appear on a constructor declaration. - - -==== tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration5.ts (1 errors) ==== - class C { - private constructor() { } - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserConstructorDeclaration5.symbols b/tests/baselines/reference/parserConstructorDeclaration5.symbols new file mode 100644 index 00000000000..6eb3d32a0f0 --- /dev/null +++ b/tests/baselines/reference/parserConstructorDeclaration5.symbols @@ -0,0 +1,6 @@ +=== tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration5.ts === +class C { +>C : Symbol(C, Decl(parserConstructorDeclaration5.ts, 0, 0)) + + private constructor() { } +} diff --git a/tests/baselines/reference/parserConstructorDeclaration5.types b/tests/baselines/reference/parserConstructorDeclaration5.types new file mode 100644 index 00000000000..ae5d2c44b49 --- /dev/null +++ b/tests/baselines/reference/parserConstructorDeclaration5.types @@ -0,0 +1,6 @@ +=== tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration5.ts === +class C { +>C : C + + private constructor() { } +} diff --git a/tests/baselines/reference/protectedMembers.errors.txt b/tests/baselines/reference/protectedMembers.errors.txt index c35bc25fe71..666be34d10c 100644 --- a/tests/baselines/reference/protectedMembers.errors.txt +++ b/tests/baselines/reference/protectedMembers.errors.txt @@ -8,16 +8,15 @@ tests/cases/compiler/protectedMembers.ts(48,1): error TS2445: Property 'sx' is p tests/cases/compiler/protectedMembers.ts(49,1): error TS2445: Property 'sf' is protected and only accessible within class 'C2' and its subclasses. tests/cases/compiler/protectedMembers.ts(68,9): error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. tests/cases/compiler/protectedMembers.ts(69,9): error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. -tests/cases/compiler/protectedMembers.ts(86,5): error TS1089: 'protected' modifier cannot appear on a constructor declaration. -tests/cases/compiler/protectedMembers.ts(98,1): error TS2322: Type 'B1' is not assignable to type 'A1'. +tests/cases/compiler/protectedMembers.ts(97,1): error TS2322: Type 'B1' is not assignable to type 'A1'. Property 'x' is protected but type 'B1' is not a class derived from 'A1'. -tests/cases/compiler/protectedMembers.ts(99,1): error TS2322: Type 'A1' is not assignable to type 'B1'. +tests/cases/compiler/protectedMembers.ts(98,1): error TS2322: Type 'A1' is not assignable to type 'B1'. Property 'x' is protected in type 'A1' but public in type 'B1'. -tests/cases/compiler/protectedMembers.ts(112,7): error TS2415: Class 'B3' incorrectly extends base class 'A3'. +tests/cases/compiler/protectedMembers.ts(111,7): error TS2415: Class 'B3' incorrectly extends base class 'A3'. Property 'x' is protected in type 'B3' but public in type 'A3'. -==== tests/cases/compiler/protectedMembers.ts (14 errors) ==== +==== tests/cases/compiler/protectedMembers.ts (13 errors) ==== // Class with protected members class C1 { protected x: number; @@ -122,10 +121,7 @@ tests/cases/compiler/protectedMembers.ts(112,7): error TS2415: Class 'B3' incorr } class CC { - // Error, constructor cannot be protected protected constructor() { - ~~~~~~~~~ -!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration. } } diff --git a/tests/baselines/reference/protectedMembers.js b/tests/baselines/reference/protectedMembers.js index e541f697f44..dcc68e0c946 100644 --- a/tests/baselines/reference/protectedMembers.js +++ b/tests/baselines/reference/protectedMembers.js @@ -83,7 +83,6 @@ interface E extends C { } class CC { - // Error, constructor cannot be protected protected constructor() { } } @@ -214,7 +213,6 @@ var D = (function (_super) { return D; }(C)); var CC = (function () { - // Error, constructor cannot be protected function CC() { } return CC; diff --git a/tests/baselines/reference/typesWithPrivateConstructor.errors.txt b/tests/baselines/reference/typesWithPrivateConstructor.errors.txt index 261038a1f4a..d30bb29e314 100644 --- a/tests/baselines/reference/typesWithPrivateConstructor.errors.txt +++ b/tests/baselines/reference/typesWithPrivateConstructor.errors.txt @@ -1,36 +1,24 @@ -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(4,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(8,5): error TS2322: Type 'Function' is not assignable to type '() => void'. - Type 'Function' provides no match for the signature '(): void' -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(11,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(12,5): error TS1089: 'private' modifier cannot appear on a constructor declaration. -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(15,10): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(6,9): error TS2673: Constructor of type '(): C' is private and only accessible within class 'C'. +tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(14,10): error TS2673: Constructor of type '(x: number): C2' is private and only accessible within class 'C2'. -==== tests/cases/conformance/types/members/typesWithPrivateConstructor.ts (5 errors) ==== - // private constructors are not allowed +==== tests/cases/conformance/types/members/typesWithPrivateConstructor.ts (2 errors) ==== class C { private constructor() { } - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. } - var c = new C(); + var c = new C(); // error C is private + ~~~~~~~ +!!! error TS2673: Constructor of type '(): C' is private and only accessible within class 'C'. var r: () => void = c.constructor; - ~ -!!! error TS2322: Type 'Function' is not assignable to type '() => void'. -!!! error TS2322: Type 'Function' provides no match for the signature '(): void' class C2 { private constructor(x: number); - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. private constructor(x: any) { } - ~~~~~~~ -!!! error TS1089: 'private' modifier cannot appear on a constructor declaration. } - var c2 = new C2(); + var c2 = new C2(); // error C2 is private ~~~~~~~~ -!!! error TS2346: Supplied parameters do not match any signature of call target. +!!! error TS2673: Constructor of type '(x: number): C2' is private and only accessible within class 'C2'. var r2: (x: number) => void = c2.constructor; \ No newline at end of file diff --git a/tests/baselines/reference/typesWithPrivateConstructor.js b/tests/baselines/reference/typesWithPrivateConstructor.js index d894eeda49a..b3111ec2a74 100644 --- a/tests/baselines/reference/typesWithPrivateConstructor.js +++ b/tests/baselines/reference/typesWithPrivateConstructor.js @@ -1,11 +1,10 @@ //// [typesWithPrivateConstructor.ts] -// private constructors are not allowed class C { private constructor() { } } -var c = new C(); +var c = new C(); // error C is private var r: () => void = c.constructor; class C2 { @@ -13,22 +12,34 @@ class C2 { private constructor(x: any) { } } -var c2 = new C2(); +var c2 = new C2(); // error C2 is private var r2: (x: number) => void = c2.constructor; //// [typesWithPrivateConstructor.js] -// private constructors are not allowed var C = (function () { function C() { } return C; }()); -var c = new C(); +var c = new C(); // error C is private var r = c.constructor; var C2 = (function () { function C2(x) { } return C2; }()); -var c2 = new C2(); +var c2 = new C2(); // error C2 is private var r2 = c2.constructor; + + +//// [typesWithPrivateConstructor.d.ts] +declare class C { + constructor(); +} +declare var c: any; +declare var r: () => void; +declare class C2 { + constructor(x); +} +declare var c2: any; +declare var r2: (x: number) => void; diff --git a/tests/baselines/reference/typesWithProtectedConstructor.errors.txt b/tests/baselines/reference/typesWithProtectedConstructor.errors.txt new file mode 100644 index 00000000000..4dc8add9fc2 --- /dev/null +++ b/tests/baselines/reference/typesWithProtectedConstructor.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(6,9): error TS2674: Constructor of type '(): C' is protected and only accessible within class 'C'. +tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(14,10): error TS2674: Constructor of type '(x: number): C2' is protected and only accessible within class 'C2'. + + +==== tests/cases/conformance/types/members/typesWithProtectedConstructor.ts (2 errors) ==== + + class C { + protected constructor() { } + } + + var c = new C(); // error C is protected + ~~~~~~~ +!!! error TS2674: Constructor of type '(): C' is protected and only accessible within class 'C'. + var r: () => void = c.constructor; + + class C2 { + protected constructor(x: number); + protected constructor(x: any) { } + } + + var c2 = new C2(); // error C2 is protected + ~~~~~~~~ +!!! error TS2674: Constructor of type '(x: number): C2' is protected and only accessible within class 'C2'. + var r2: (x: number) => void = c2.constructor; \ No newline at end of file diff --git a/tests/baselines/reference/typesWithProtectedConstructor.js b/tests/baselines/reference/typesWithProtectedConstructor.js new file mode 100644 index 00000000000..02f29464533 --- /dev/null +++ b/tests/baselines/reference/typesWithProtectedConstructor.js @@ -0,0 +1,45 @@ +//// [typesWithProtectedConstructor.ts] + +class C { + protected constructor() { } +} + +var c = new C(); // error C is protected +var r: () => void = c.constructor; + +class C2 { + protected constructor(x: number); + protected constructor(x: any) { } +} + +var c2 = new C2(); // error C2 is protected +var r2: (x: number) => void = c2.constructor; + +//// [typesWithProtectedConstructor.js] +var C = (function () { + function C() { + } + return C; +}()); +var c = new C(); // error C is protected +var r = c.constructor; +var C2 = (function () { + function C2(x) { + } + return C2; +}()); +var c2 = new C2(); // error C2 is protected +var r2 = c2.constructor; + + +//// [typesWithProtectedConstructor.d.ts] +declare class C { + constructor(); +} +declare var c: any; +declare var r: () => void; +declare class C2 { + constructor(x: number); +} +declare var c2: any; +declare var r2: (x: number) => void; diff --git a/tests/cases/compiler/protectedMembers.ts b/tests/cases/compiler/protectedMembers.ts index ae32e472425..b7528f26068 100644 --- a/tests/cases/compiler/protectedMembers.ts +++ b/tests/cases/compiler/protectedMembers.ts @@ -82,7 +82,6 @@ interface E extends C { } class CC { - // Error, constructor cannot be protected protected constructor() { } } diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts index c2e4855e63a..1c9a801429d 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts @@ -1,18 +1,20 @@ +// @declaration: true + class C { public constructor(public x: number) { } } class D { - private constructor(public x: number) { } // error + private constructor(public x: number) { } } class E { - protected constructor(public x: number) { } // error + protected constructor(public x: number) { } } var c = new C(1); -var d = new D(1); -var e = new E(1); +var d = new D(1); // error +var e = new E(1); // error module Generic { class C { @@ -20,14 +22,14 @@ module Generic { } class D { - private constructor(public x: T) { } // error + private constructor(public x: T) { } } class E { - protected constructor(public x: T) { } // error + protected constructor(public x: T) { } } var c = new C(1); - var d = new D(1); - var e = new E(1); + var d = new D(1); // error + var e = new E(1); // error } diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts new file mode 100644 index 00000000000..2a961e2c067 --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts @@ -0,0 +1,42 @@ +// @declaration: true + +class BaseA { + public constructor(public x: number) { } + createInstance() { new BaseA(1); } +} + +class BaseB { + protected constructor(public x: number) { } + createInstance() { new BaseB(1); } +} + +class BaseC { + private constructor(public x: number) { } + createInstance() { new BaseC(1); } +} + +class DerivedA extends BaseA { + constructor(public x: number) { super(x); } + createInstance() { new DerivedA(1); } + createBaseInstance() { new BaseA(1); } +} + +class DerivedB extends BaseB { + constructor(public x: number) { super(x); } + createInstance() { new DerivedB(1); } + createBaseInstance() { new BaseB(1); } // error +} + +class DerivedC extends BaseC { // error + constructor(public x: number) { super(x); } + createInstance() { new DerivedC(1); } + createBaseInstance() { new BaseC(1); } // error +} + +var ba = new BaseA(1); +var bb = new BaseB(1); // error +var bc = new BaseC(1); // error + +var da = new DerivedA(1); +var db = new DerivedB(1); +var dc = new DerivedC(1); \ No newline at end of file diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts new file mode 100644 index 00000000000..8508a40d101 --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts @@ -0,0 +1,35 @@ +// @declaration: true + +class Foo { + constructor(public x: number) { } +} + +class Bar { + public constructor(public x: number) { } +} + +class Baz { + protected constructor(public x: number) { } +} + +class Qux { + private constructor(public x: number) { } +} + +// b is public +let a = Foo; +a = Bar; +a = Baz; // error Baz is protected +a = Qux; // error Qux is private + +// b is protected +let b = Baz; +b = Foo; // error Foo is public +b = Bar; // error Baz is public +b = Qux; // error Qux is private + +// c is private +let c = Qux; +c = Foo; // error Foo is public +c = Bar; // error Bar is public +c = Baz; // error Baz is protected \ No newline at end of file diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts new file mode 100644 index 00000000000..5ed20d03853 --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorOverloadsAccessibility.ts @@ -0,0 +1,34 @@ +// @declaration: true + +class A { + public constructor(a: boolean) // error + protected constructor(a: number) // error + private constructor(a: string) + private constructor() { + + } +} + +class B { + protected constructor(a: number) // error + constructor(a: string) + constructor() { + + } +} + +class C { + protected constructor(a: number) + protected constructor(a: string) + protected constructor() { + + } +} + +class D { + constructor(a: number) + constructor(a: string) + public constructor() { + + } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/members/typesWithPrivateConstructor.ts b/tests/cases/conformance/types/members/typesWithPrivateConstructor.ts index 4fb24ca58e4..321864e1751 100644 --- a/tests/cases/conformance/types/members/typesWithPrivateConstructor.ts +++ b/tests/cases/conformance/types/members/typesWithPrivateConstructor.ts @@ -1,10 +1,10 @@ -// private constructors are not allowed +// @declaration: true class C { private constructor() { } } -var c = new C(); +var c = new C(); // error C is private var r: () => void = c.constructor; class C2 { @@ -12,5 +12,5 @@ class C2 { private constructor(x: any) { } } -var c2 = new C2(); +var c2 = new C2(); // error C2 is private var r2: (x: number) => void = c2.constructor; \ No newline at end of file diff --git a/tests/cases/conformance/types/members/typesWithProtectedConstructor.ts b/tests/cases/conformance/types/members/typesWithProtectedConstructor.ts new file mode 100644 index 00000000000..e7f6adc38a8 --- /dev/null +++ b/tests/cases/conformance/types/members/typesWithProtectedConstructor.ts @@ -0,0 +1,16 @@ +// @declaration: true + +class C { + protected constructor() { } +} + +var c = new C(); // error C is protected +var r: () => void = c.constructor; + +class C2 { + protected constructor(x: number); + protected constructor(x: any) { } +} + +var c2 = new C2(); // error C2 is protected +var r2: (x: number) => void = c2.constructor; \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoOnPrivateConstructorCall.ts b/tests/cases/fourslash/quickInfoOnPrivateConstructorCall.ts new file mode 100644 index 00000000000..5ebdcd815b2 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnPrivateConstructorCall.ts @@ -0,0 +1,9 @@ +/// + +////class A { +//// private constructor() {} +////} +////var x = new A(/*1*/ + +goTo.marker("1"); +verify.not.signatureHelpPresent(); \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoOnProtectedConstructorCall.ts b/tests/cases/fourslash/quickInfoOnProtectedConstructorCall.ts new file mode 100644 index 00000000000..094a07f3d51 --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnProtectedConstructorCall.ts @@ -0,0 +1,9 @@ +/// + +////class A { +//// protected constructor() {} +////} +////var x = new A(/*1*/ + +goTo.marker("1"); +verify.not.signatureHelpPresent(); \ No newline at end of file From bbf92ce39187727bd8e547af7a4bfaa957ae2e2d Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 18:32:56 +0000 Subject: [PATCH 03/55] Added constructor visibility in the declaration emitter --- src/compiler/declarationEmitter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index ab0b16947fc..2963ddf6c58 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -1316,7 +1316,7 @@ namespace ts { if (node.kind === SyntaxKind.FunctionDeclaration) { emitModuleElementDeclarationFlags(node); } - else if (node.kind === SyntaxKind.MethodDeclaration) { + else if (node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.Constructor) { emitClassMemberDeclarationFlags(node.flags); } if (node.kind === SyntaxKind.FunctionDeclaration) { From 3d529a84c533dc382b1203152d3e45d43a04619b Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 18:33:53 +0000 Subject: [PATCH 04/55] Update baselines --- .../reference/classConstructorAccessibility.js | 4 ++-- .../reference/classConstructorAccessibility2.js | 4 ++-- .../reference/classConstructorAccessibility3.js | 4 ++-- .../classConstructorOverloadsAccessibility.js | 10 +++++----- .../baselines/reference/typesWithPrivateConstructor.js | 4 ++-- .../reference/typesWithProtectedConstructor.js | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/baselines/reference/classConstructorAccessibility.js b/tests/baselines/reference/classConstructorAccessibility.js index de7e4a83bb8..efc3f3c273c 100644 --- a/tests/baselines/reference/classConstructorAccessibility.js +++ b/tests/baselines/reference/classConstructorAccessibility.js @@ -90,11 +90,11 @@ declare class C { } declare class D { x: number; - constructor(x); + private constructor(x); } declare class E { x: number; - constructor(x: number); + protected constructor(x: number); } declare var c: C; declare var d: any; diff --git a/tests/baselines/reference/classConstructorAccessibility2.js b/tests/baselines/reference/classConstructorAccessibility2.js index 47c0b0bc626..e19589ece45 100644 --- a/tests/baselines/reference/classConstructorAccessibility2.js +++ b/tests/baselines/reference/classConstructorAccessibility2.js @@ -114,12 +114,12 @@ declare class BaseA { } declare class BaseB { x: number; - constructor(x: number); + protected constructor(x: number); createInstance(): void; } declare class BaseC { x: number; - constructor(x); + private constructor(x); createInstance(): void; } declare class DerivedA extends BaseA { diff --git a/tests/baselines/reference/classConstructorAccessibility3.js b/tests/baselines/reference/classConstructorAccessibility3.js index 8e900b6eb2d..02f5bb45a86 100644 --- a/tests/baselines/reference/classConstructorAccessibility3.js +++ b/tests/baselines/reference/classConstructorAccessibility3.js @@ -87,11 +87,11 @@ declare class Bar { } declare class Baz { x: number; - constructor(x: number); + protected constructor(x: number); } declare class Qux { x: number; - constructor(x); + private constructor(x); } declare let a: typeof Foo; declare let b: typeof Baz; diff --git a/tests/baselines/reference/classConstructorOverloadsAccessibility.js b/tests/baselines/reference/classConstructorOverloadsAccessibility.js index b69257a1c2a..3914c29b785 100644 --- a/tests/baselines/reference/classConstructorOverloadsAccessibility.js +++ b/tests/baselines/reference/classConstructorOverloadsAccessibility.js @@ -59,16 +59,16 @@ var D = (function () { //// [classConstructorOverloadsAccessibility.d.ts] declare class A { constructor(a: boolean); - constructor(a: number); - constructor(a); + protected constructor(a: number); + private constructor(a); } declare class B { - constructor(a: number); + protected constructor(a: number); constructor(a: string); } declare class C { - constructor(a: number); - constructor(a: string); + protected constructor(a: number); + protected constructor(a: string); } declare class D { constructor(a: number); diff --git a/tests/baselines/reference/typesWithPrivateConstructor.js b/tests/baselines/reference/typesWithPrivateConstructor.js index b3111ec2a74..1795ee6b316 100644 --- a/tests/baselines/reference/typesWithPrivateConstructor.js +++ b/tests/baselines/reference/typesWithPrivateConstructor.js @@ -34,12 +34,12 @@ var r2 = c2.constructor; //// [typesWithPrivateConstructor.d.ts] declare class C { - constructor(); + private constructor(); } declare var c: any; declare var r: () => void; declare class C2 { - constructor(x); + private constructor(x); } declare var c2: any; declare var r2: (x: number) => void; diff --git a/tests/baselines/reference/typesWithProtectedConstructor.js b/tests/baselines/reference/typesWithProtectedConstructor.js index 02f29464533..e75b3b6ecd3 100644 --- a/tests/baselines/reference/typesWithProtectedConstructor.js +++ b/tests/baselines/reference/typesWithProtectedConstructor.js @@ -34,12 +34,12 @@ var r2 = c2.constructor; //// [typesWithProtectedConstructor.d.ts] declare class C { - constructor(); + protected constructor(); } declare var c: any; declare var r: () => void; declare class C2 { - constructor(x: number); + protected constructor(x: number); } declare var c2: any; declare var r2: (x: number) => void; From 42000519347ecefe60687b9976e654781a69bbec Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 21:00:37 +0000 Subject: [PATCH 05/55] Addressed PR feedback. --- src/compiler/checker.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1ce5b51c66d..ebb02a2fdad 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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) { + // 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) + || (targetAccessibility !== NodeFlags.Protected && !(sourceAccessibility & (NodeFlags.Private | NodeFlags.Protected))); + 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"; - } } } From 4c93eb2e98703ae86c81721d28b03babdf24bc61 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 21:00:50 +0000 Subject: [PATCH 06/55] Accept baselines --- .../classConstructorAccessibility3.errors.txt | 37 +++---------------- .../classConstructorAccessibility3.js | 20 +++++----- .../classConstructorAccessibility3.ts | 10 ++--- 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/tests/baselines/reference/classConstructorAccessibility3.errors.txt b/tests/baselines/reference/classConstructorAccessibility3.errors.txt index c536db1f9da..1b96db289dc 100644 --- a/tests/baselines/reference/classConstructorAccessibility3.errors.txt +++ b/tests/baselines/reference/classConstructorAccessibility3.errors.txt @@ -2,21 +2,11 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib Cannot assign a 'protected' constructor type to a 'public' constructor type. tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(22,1): error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Foo'. Cannot assign a 'private' constructor type to a 'public' constructor type. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(26,1): error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Baz'. - Cannot assign a 'public' constructor type to a 'protected' constructor type. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(27,1): error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Baz'. - Cannot assign a 'public' constructor type to a 'protected' constructor type. tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(28,1): error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Baz'. Cannot assign a 'private' constructor type to a 'protected' constructor type. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(32,1): error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Qux'. - Cannot assign a 'public' constructor type to a 'private' constructor type. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(33,1): error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Qux'. - Cannot assign a 'public' constructor type to a 'private' constructor type. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts(34,1): error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Qux'. - Cannot assign a 'protected' constructor type to a 'private' constructor type. -==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts (8 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts (3 errors) ==== class Foo { constructor(public x: number) { } @@ -48,14 +38,8 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib // b is protected let b = Baz; - b = Foo; // error Foo is public - ~ -!!! error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Baz'. -!!! error TS2322: Cannot assign a 'public' constructor type to a 'protected' constructor type. - b = Bar; // error Baz is public - ~ -!!! error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Baz'. -!!! error TS2322: Cannot assign a 'public' constructor type to a 'protected' constructor type. + b = Foo; + b = Bar; b = Qux; // error Qux is private ~ !!! error TS2322: Type 'typeof Qux' is not assignable to type 'typeof Baz'. @@ -63,15 +47,6 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib // c is private let c = Qux; - c = Foo; // error Foo is public - ~ -!!! error TS2322: Type 'typeof Foo' is not assignable to type 'typeof Qux'. -!!! error TS2322: Cannot assign a 'public' constructor type to a 'private' constructor type. - c = Bar; // error Bar is public - ~ -!!! error TS2322: Type 'typeof Bar' is not assignable to type 'typeof Qux'. -!!! error TS2322: Cannot assign a 'public' constructor type to a 'private' constructor type. - c = Baz; // error Baz is protected - ~ -!!! error TS2322: Type 'typeof Baz' is not assignable to type 'typeof Qux'. -!!! error TS2322: Cannot assign a 'protected' constructor type to a 'private' constructor type. \ No newline at end of file + c = Foo; + c = Bar; + c = Baz; \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility3.js b/tests/baselines/reference/classConstructorAccessibility3.js index 02f5bb45a86..acc7d38aa55 100644 --- a/tests/baselines/reference/classConstructorAccessibility3.js +++ b/tests/baselines/reference/classConstructorAccessibility3.js @@ -24,15 +24,15 @@ a = Qux; // error Qux is private // b is protected let b = Baz; -b = Foo; // error Foo is public -b = Bar; // error Baz is public +b = Foo; +b = Bar; b = Qux; // error Qux is private // c is private let c = Qux; -c = Foo; // error Foo is public -c = Bar; // error Bar is public -c = Baz; // error Baz is protected +c = Foo; +c = Bar; +c = Baz; //// [classConstructorAccessibility3.js] var Foo = (function () { @@ -66,14 +66,14 @@ a = Baz; // error Baz is protected a = Qux; // error Qux is private // b is protected var b = Baz; -b = Foo; // error Foo is public -b = Bar; // error Baz is public +b = Foo; +b = Bar; b = Qux; // error Qux is private // c is private var c = Qux; -c = Foo; // error Foo is public -c = Bar; // error Bar is public -c = Baz; // error Baz is protected +c = Foo; +c = Bar; +c = Baz; //// [classConstructorAccessibility3.d.ts] diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts index 8508a40d101..91bb4873d96 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility3.ts @@ -24,12 +24,12 @@ a = Qux; // error Qux is private // b is protected let b = Baz; -b = Foo; // error Foo is public -b = Bar; // error Baz is public +b = Foo; +b = Bar; b = Qux; // error Qux is private // c is private let c = Qux; -c = Foo; // error Foo is public -c = Bar; // error Bar is public -c = Baz; // error Baz is protected \ No newline at end of file +c = Foo; +c = Bar; +c = Baz; \ No newline at end of file From 5ce0202bc9b93ba53d61f5e298a30dfd9fd1beb2 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 21:46:28 +0000 Subject: [PATCH 07/55] Refactored visibility text on modifier grammar check --- src/compiler/checker.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ebb02a2fdad..2351f6913af 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16269,16 +16269,12 @@ namespace ts { case SyntaxKind.PublicKeyword: case SyntaxKind.ProtectedKeyword: case SyntaxKind.PrivateKeyword: - let text: string; - if (modifier.kind === SyntaxKind.PublicKeyword) { - text = "public"; - } - else if (modifier.kind === SyntaxKind.ProtectedKeyword) { - text = "protected"; + let text = visibilityToString(modifierToFlag(modifier.kind)); + + if (modifier.kind === SyntaxKind.ProtectedKeyword) { lastProtected = modifier; } - else { - text = "private"; + else if (modifier.kind === SyntaxKind.PrivateKeyword) { lastPrivate = modifier; } From 16b54e0d9fced4351fa8f2431150be29ddd5bfcc Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 22:52:19 +0000 Subject: [PATCH 08/55] Addressed PR. Diagnostic messages --- src/compiler/checker.ts | 6 +++--- src/compiler/diagnosticMessages.json | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2351f6913af..5c69bb7f356 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10182,10 +10182,10 @@ namespace ts { // A private or protected constructor can only be instantiated within it's own class if (declaringClass !== enclosingClass) { if (flags & NodeFlags.Private) { - error(node, Diagnostics.Constructor_of_type_0_is_private_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); + error(node, Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); } if (flags & NodeFlags.Protected) { - error(node, Diagnostics.Constructor_of_type_0_is_protected_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); + error(node, Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); } return false; } @@ -14059,7 +14059,7 @@ namespace ts { if (signatures.length) { const declaration = signatures[0].declaration; if (declaration && declaration.flags & NodeFlags.Private) { - error(node, Diagnostics.Cannot_extend_private_class_0, (node.expression).text); + error(node, Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, (node.expression).text); } } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 2ee388ff42e..b88b9b2a0e1 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1827,15 +1827,15 @@ "category": "Error", "code": 2672 }, - "Constructor of type '{0}' is private and only accessible within class '{1}'.": { + "Constructor of class '{0}' is private and only accessible within the class declaration.": { "category": "Error", "code": 2673 }, - "Constructor of type '{0}' is protected and only accessible within class '{1}'.": { + "Constructor of class '{0}' is protected and only accessible within the class declaration.": { "category": "Error", "code": 2674 }, - "Cannot extend private class '{0}'.": { + "Cannot extend a class '{0}'. Class constructor is marked as private.": { "category": "Error", "code": 2675 }, From 037b65781fe268fd3f6206a16a27dfbc7ee4b652 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 4 Feb 2016 22:52:38 +0000 Subject: [PATCH 09/55] Accept baselines --- .../classConstructorAccessibility.errors.txt | 16 +++++++-------- .../classConstructorAccessibility2.errors.txt | 20 +++++++++---------- .../typesWithPrivateConstructor.errors.txt | 8 ++++---- .../typesWithProtectedConstructor.errors.txt | 8 ++++---- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/baselines/reference/classConstructorAccessibility.errors.txt b/tests/baselines/reference/classConstructorAccessibility.errors.txt index a24ec6c63d1..c79633ba33d 100644 --- a/tests/baselines/reference/classConstructorAccessibility.errors.txt +++ b/tests/baselines/reference/classConstructorAccessibility.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(15,9): error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(16,9): error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(32,13): error TS2673: Constructor of type '(x: T): D' is private and only accessible within class 'D'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(33,13): error TS2674: Constructor of type '(x: T): E' is protected and only accessible within class 'E'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(15,9): error TS2673: Constructor of class 'D' is private and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(16,9): error TS2674: Constructor of class 'E' is protected and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(32,13): error TS2673: Constructor of class 'D' is private and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(33,13): error TS2674: Constructor of class 'E' is protected and only accessible within the class declaration. ==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ==== @@ -21,10 +21,10 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib var c = new C(1); var d = new D(1); // error ~~~~~~~~ -!!! error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'. +!!! error TS2673: Constructor of class 'D' is private and only accessible within the class declaration. var e = new E(1); // error ~~~~~~~~ -!!! error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'. +!!! error TS2674: Constructor of class 'E' is protected and only accessible within the class declaration. module Generic { class C { @@ -42,9 +42,9 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib var c = new C(1); var d = new D(1); // error ~~~~~~~~ -!!! error TS2673: Constructor of type '(x: T): D' is private and only accessible within class 'D'. +!!! error TS2673: Constructor of class 'D' is private and only accessible within the class declaration. var e = new E(1); // error ~~~~~~~~ -!!! error TS2674: Constructor of type '(x: T): E' is protected and only accessible within class 'E'. +!!! error TS2674: Constructor of class 'E' is protected and only accessible within the class declaration. } \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility2.errors.txt b/tests/baselines/reference/classConstructorAccessibility2.errors.txt index 1c6fc46edb3..7bd784ac268 100644 --- a/tests/baselines/reference/classConstructorAccessibility2.errors.txt +++ b/tests/baselines/reference/classConstructorAccessibility2.errors.txt @@ -1,8 +1,8 @@ -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(26,28): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend private class 'BaseC'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,28): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,10): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(37,10): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(26,28): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,28): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,10): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(37,10): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration. ==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ==== @@ -33,26 +33,26 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib createInstance() { new DerivedB(1); } createBaseInstance() { new BaseB(1); } // error ~~~~~~~~~~~~ -!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. +!!! error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration. } class DerivedC extends BaseC { // error ~~~~~ -!!! error TS2675: Cannot extend private class 'BaseC'. +!!! error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private. constructor(public x: number) { super(x); } createInstance() { new DerivedC(1); } createBaseInstance() { new BaseC(1); } // error ~~~~~~~~~~~~ -!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. +!!! error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration. } var ba = new BaseA(1); var bb = new BaseB(1); // error ~~~~~~~~~~~~ -!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'. +!!! error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration. var bc = new BaseC(1); // error ~~~~~~~~~~~~ -!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'. +!!! error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration. var da = new DerivedA(1); var db = new DerivedB(1); diff --git a/tests/baselines/reference/typesWithPrivateConstructor.errors.txt b/tests/baselines/reference/typesWithPrivateConstructor.errors.txt index d30bb29e314..ab8ea4e26ad 100644 --- a/tests/baselines/reference/typesWithPrivateConstructor.errors.txt +++ b/tests/baselines/reference/typesWithPrivateConstructor.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(6,9): error TS2673: Constructor of type '(): C' is private and only accessible within class 'C'. -tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(14,10): error TS2673: Constructor of type '(x: number): C2' is private and only accessible within class 'C2'. +tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(6,9): error TS2673: Constructor of class 'C' is private and only accessible within the class declaration. +tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(14,10): error TS2673: Constructor of class 'C2' is private and only accessible within the class declaration. ==== tests/cases/conformance/types/members/typesWithPrivateConstructor.ts (2 errors) ==== @@ -10,7 +10,7 @@ tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(14,10): err var c = new C(); // error C is private ~~~~~~~ -!!! error TS2673: Constructor of type '(): C' is private and only accessible within class 'C'. +!!! error TS2673: Constructor of class 'C' is private and only accessible within the class declaration. var r: () => void = c.constructor; class C2 { @@ -20,5 +20,5 @@ tests/cases/conformance/types/members/typesWithPrivateConstructor.ts(14,10): err var c2 = new C2(); // error C2 is private ~~~~~~~~ -!!! error TS2673: Constructor of type '(x: number): C2' is private and only accessible within class 'C2'. +!!! error TS2673: Constructor of class 'C2' is private and only accessible within the class declaration. var r2: (x: number) => void = c2.constructor; \ No newline at end of file diff --git a/tests/baselines/reference/typesWithProtectedConstructor.errors.txt b/tests/baselines/reference/typesWithProtectedConstructor.errors.txt index 4dc8add9fc2..2a5a059f44d 100644 --- a/tests/baselines/reference/typesWithProtectedConstructor.errors.txt +++ b/tests/baselines/reference/typesWithProtectedConstructor.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(6,9): error TS2674: Constructor of type '(): C' is protected and only accessible within class 'C'. -tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(14,10): error TS2674: Constructor of type '(x: number): C2' is protected and only accessible within class 'C2'. +tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(6,9): error TS2674: Constructor of class 'C' is protected and only accessible within the class declaration. +tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(14,10): error TS2674: Constructor of class 'C2' is protected and only accessible within the class declaration. ==== tests/cases/conformance/types/members/typesWithProtectedConstructor.ts (2 errors) ==== @@ -10,7 +10,7 @@ tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(14,10): e var c = new C(); // error C is protected ~~~~~~~ -!!! error TS2674: Constructor of type '(): C' is protected and only accessible within class 'C'. +!!! error TS2674: Constructor of class 'C' is protected and only accessible within the class declaration. var r: () => void = c.constructor; class C2 { @@ -20,5 +20,5 @@ tests/cases/conformance/types/members/typesWithProtectedConstructor.ts(14,10): e var c2 = new C2(); // error C2 is protected ~~~~~~~~ -!!! error TS2674: Constructor of type '(x: number): C2' is protected and only accessible within class 'C2'. +!!! error TS2674: Constructor of class 'C2' is protected and only accessible within the class declaration. var r2: (x: number) => void = c2.constructor; \ No newline at end of file From a6a5a22a32e99c2bfd2d7eb658e7e6ce8c9588b3 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Fri, 5 Feb 2016 18:53:08 +0000 Subject: [PATCH 10/55] Addressed PR --- src/compiler/checker.ts | 46 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5c69bb7f356..9a7a92b91ec 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5910,7 +5910,7 @@ namespace ts { } return Ternary.False; } - if (!constructorRelatedTo(sourceSignatures[0], targetSignatures[0], reportErrors)) { + if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) { return Ternary.False; } } @@ -6067,26 +6067,34 @@ namespace ts { return Ternary.True; } - function constructorRelatedTo(sourceSignature: Signature, targetSignature: Signature, reportErrors: boolean) { - if (sourceSignature && targetSignature && sourceSignature.declaration && targetSignature.declaration) { - // 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 = targetAccessibility === NodeFlags.Private - || (targetAccessibility === NodeFlags.Protected && sourceAccessibility !== NodeFlags.Private) - || (targetAccessibility !== NodeFlags.Protected && !(sourceAccessibility & (NodeFlags.Private | NodeFlags.Protected))); - - if (!isRelated && reportErrors) { - reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); - } - - return isRelated; + function constructorVisibilitiesAreCompatible(sourceSignature: Signature, targetSignature: Signature, reportErrors: boolean) { + if (!sourceSignature.declaration || !targetSignature.declaration) { + return true; } - return true; + const sourceAccessibility = sourceSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); + const targetAccessibility = targetSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); + + // 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; } } From 1d428b9fe01a83b334bdad4562295b12345a4786 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 9 Feb 2016 14:31:09 -0800 Subject: [PATCH 11/55] Implicit index signatures for object literal types --- src/compiler/checker.ts | 69 ++++++++++++++++++++++++---- src/compiler/diagnosticMessages.json | 4 ++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b198b827d0c..2f236ed6e44 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3987,6 +3987,19 @@ namespace ts { return getIndexTypeOfStructuredType(getApparentType(type), kind); } + function getImplicitIndexTypeOfType(type: Type, kind: IndexKind): Type { + if (isObjectLiteralType(type)) { + const propTypes: Type[] = []; + for (const prop of getPropertiesOfType(type)) { + if (kind === IndexKind.String || isNumericLiteralName(prop.name)) { + propTypes.push(getTypeOfSymbol(prop)); + } + } + return getUnionType(propTypes); + } + return undefined; + } + function getTypeParametersFromJSDocTemplate(declaration: SignatureDeclaration): TypeParameter[] { if (declaration.flags & NodeFlags.JavaScriptFile) { const templateTag = getJSDocTemplateTag(declaration); @@ -5951,6 +5964,23 @@ namespace ts { return result; } + function eachPropertyRelatedTo(source: Type, target: Type, numericPropertiesOnly: boolean, reportErrors: boolean): Ternary { + let result = Ternary.True; + for (const prop of getPropertiesOfObjectType(source)) { + if (!numericPropertiesOnly || isNumericLiteralName(prop.name)) { + const related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); + if (!related) { + if (reportErrors) { + reportError(Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop)); + } + return Ternary.False; + } + result &= related; + } + } + return result; + } + function stringIndexTypesRelatedTo(source: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { if (relation === identityRelation) { return indexTypesIdenticalTo(IndexKind.String, source, target); @@ -5964,6 +5994,9 @@ namespace ts { } const sourceInfo = getIndexInfoOfType(source, IndexKind.String); if (!sourceInfo) { + if (isObjectLiteralType(source)) { + return eachPropertyRelatedTo(source, targetInfo.type, /* numericPropertiesOnly*/ false, reportErrors); + } if (reportErrors) { reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); } @@ -5995,6 +6028,9 @@ namespace ts { const sourceStringInfo = getIndexInfoOfType(source, IndexKind.String); const sourceNumberInfo = getIndexInfoOfType(source, IndexKind.Number); if (!(sourceStringInfo || sourceNumberInfo)) { + if (isObjectLiteralType(source)) { + return eachPropertyRelatedTo(source, targetInfo.type, /* numericPropertiesOnly*/ true, reportErrors); + } if (reportErrors) { reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); } @@ -6263,6 +6299,15 @@ namespace ts { return !!(type.flags & TypeFlags.Tuple); } + /** + * Return true if type was inferred from an object literal or written as an object type literal + */ + function isObjectLiteralType(type: Type) { + return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral)) !== 0 && + getSignaturesOfType(type, SignatureKind.Call).length === 0 && + getSignaturesOfType(type, SignatureKind.Construct).length === 0; + } + function getRegularTypeOfObjectLiteral(type: Type): Type { if (type.flags & TypeFlags.FreshObjectLiteral) { let regularType = (type).regularType; @@ -6610,9 +6655,7 @@ namespace ts { inferFromProperties(source, target); inferFromSignatures(source, target, SignatureKind.Call); inferFromSignatures(source, target, SignatureKind.Construct); - inferFromIndexTypes(source, target, IndexKind.String, IndexKind.String); - inferFromIndexTypes(source, target, IndexKind.Number, IndexKind.Number); - inferFromIndexTypes(source, target, IndexKind.String, IndexKind.Number); + inferFromIndexTypes(source, target); depth--; } } @@ -6644,12 +6687,22 @@ namespace ts { inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } - function inferFromIndexTypes(source: Type, target: Type, sourceKind: IndexKind, targetKind: IndexKind) { - const targetIndexType = getIndexTypeOfType(target, targetKind); - if (targetIndexType) { - const sourceIndexType = getIndexTypeOfType(source, sourceKind); + function inferFromIndexTypes(source: Type, target: Type) { + const targetStringIndexType = getIndexTypeOfType(target, IndexKind.String); + if (targetStringIndexType) { + const sourceIndexType = getIndexTypeOfType(source, IndexKind.String) || + getImplicitIndexTypeOfType(source, IndexKind.String); if (sourceIndexType) { - inferFromTypes(sourceIndexType, targetIndexType); + inferFromTypes(sourceIndexType, targetStringIndexType); + } + } + const targetNumberIndexType = getIndexTypeOfType(target, IndexKind.Number); + if (targetNumberIndexType) { + const sourceIndexType = getIndexTypeOfType(source, IndexKind.Number) || + getIndexTypeOfType(source, IndexKind.String) || + getImplicitIndexTypeOfType(source, IndexKind.Number); + if (sourceIndexType) { + inferFromTypes(sourceIndexType, targetNumberIndexType); } } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 5b93f61bb90..2458e68f23c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1699,6 +1699,10 @@ "category": "Error", "code": 2529 }, + "Property '{0}' is incompatible with index signature.": { + "category": "Error", + "code": 2530 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600 From 837e6dbda36cdf34befbf0536592f4913c59d645 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 9 Feb 2016 14:33:24 -0800 Subject: [PATCH 12/55] Accepting new baselines --- ...eAssignmentCompatIndexSignature.errors.txt | 7 +-- ...tionshipObjectsOnIndexSignature.errors.txt | 52 +------------------ ...ontextualTypingOfObjectLiterals.errors.txt | 23 -------- .../contextualTypingOfObjectLiterals.symbols | 32 ++++++++++++ .../contextualTypingOfObjectLiterals.types | 41 +++++++++++++++ .../reference/indexerAssignability.errors.txt | 28 ---------- .../reference/indexerAssignability.symbols | 36 +++++++++++++ .../reference/indexerAssignability.types | 42 +++++++++++++++ .../numericIndexerConstraint2.errors.txt | 6 ++- .../numericIndexerConstraint5.errors.txt | 6 ++- .../stringIndexerAssignments1.errors.txt | 7 +-- 11 files changed, 163 insertions(+), 117 deletions(-) delete mode 100644 tests/baselines/reference/contextualTypingOfObjectLiterals.errors.txt create mode 100644 tests/baselines/reference/contextualTypingOfObjectLiterals.symbols create mode 100644 tests/baselines/reference/contextualTypingOfObjectLiterals.types delete mode 100644 tests/baselines/reference/indexerAssignability.errors.txt create mode 100644 tests/baselines/reference/indexerAssignability.symbols create mode 100644 tests/baselines/reference/indexerAssignability.types diff --git a/tests/baselines/reference/augmentedTypeAssignmentCompatIndexSignature.errors.txt b/tests/baselines/reference/augmentedTypeAssignmentCompatIndexSignature.errors.txt index eb5020e97f9..e99cac7968c 100644 --- a/tests/baselines/reference/augmentedTypeAssignmentCompatIndexSignature.errors.txt +++ b/tests/baselines/reference/augmentedTypeAssignmentCompatIndexSignature.errors.txt @@ -1,10 +1,8 @@ -tests/cases/conformance/types/members/augmentedTypeAssignmentCompatIndexSignature.ts(15,5): error TS2322: Type '{}' is not assignable to type '{ [n: number]: Foo; }'. - Index signature is missing in type '{}'. tests/cases/conformance/types/members/augmentedTypeAssignmentCompatIndexSignature.ts(19,5): error TS2322: Type '() => void' is not assignable to type '{ [n: number]: Bar; }'. Index signature is missing in type '() => void'. -==== tests/cases/conformance/types/members/augmentedTypeAssignmentCompatIndexSignature.ts (2 errors) ==== +==== tests/cases/conformance/types/members/augmentedTypeAssignmentCompatIndexSignature.ts (1 errors) ==== interface Foo { a } interface Bar { b } @@ -20,9 +18,6 @@ tests/cases/conformance/types/members/augmentedTypeAssignmentCompatIndexSignatur var f = () => { }; var v1: { - ~~ -!!! error TS2322: Type '{}' is not assignable to type '{ [n: number]: Foo; }'. -!!! error TS2322: Index signature is missing in type '{}'. [n: number]: Foo } = o; // Should be allowed diff --git a/tests/baselines/reference/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.errors.txt b/tests/baselines/reference/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.errors.txt index fe608b14379..2efaf2e5ae2 100644 --- a/tests/baselines/reference/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.errors.txt +++ b/tests/baselines/reference/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.errors.txt @@ -1,70 +1,54 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(26,12): error TS2365: Operator '<' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(27,12): error TS2365: Operator '<' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(28,12): error TS2365: Operator '<' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(29,12): error TS2365: Operator '<' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(31,12): error TS2365: Operator '<' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(32,12): error TS2365: Operator '<' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(33,12): error TS2365: Operator '<' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(34,12): error TS2365: Operator '<' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(37,12): error TS2365: Operator '>' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(38,12): error TS2365: Operator '>' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(39,12): error TS2365: Operator '>' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(40,12): error TS2365: Operator '>' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(42,12): error TS2365: Operator '>' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(43,12): error TS2365: Operator '>' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(44,12): error TS2365: Operator '>' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(45,12): error TS2365: Operator '>' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(48,12): error TS2365: Operator '<=' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(49,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(50,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(51,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(53,12): error TS2365: Operator '<=' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(54,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(55,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(56,12): error TS2365: Operator '<=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(59,12): error TS2365: Operator '>=' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(60,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(61,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(62,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(64,12): error TS2365: Operator '>=' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(65,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(66,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(67,12): error TS2365: Operator '>=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(70,12): error TS2365: Operator '==' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(71,12): error TS2365: Operator '==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(72,12): error TS2365: Operator '==' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(73,12): error TS2365: Operator '==' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(75,12): error TS2365: Operator '==' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(76,12): error TS2365: Operator '==' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(77,12): error TS2365: Operator '==' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(78,12): error TS2365: Operator '==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(81,12): error TS2365: Operator '!=' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(82,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(83,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(84,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(86,12): error TS2365: Operator '!=' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(87,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(88,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(89,12): error TS2365: Operator '!=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(92,12): error TS2365: Operator '===' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(93,12): error TS2365: Operator '===' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(94,12): error TS2365: Operator '===' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(95,12): error TS2365: Operator '===' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(97,12): error TS2365: Operator '===' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(98,12): error TS2365: Operator '===' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(99,12): error TS2365: Operator '===' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(100,12): error TS2365: Operator '===' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(103,12): error TS2365: Operator '!==' cannot be applied to types '{ [a: string]: string; }' and '{ [b: string]: number; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(104,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: string]: C; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(105,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(106,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(108,12): error TS2365: Operator '!==' cannot be applied to types '{ [b: string]: number; }' and '{ [a: string]: string; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(109,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: string]: C; }' and '{ [index: string]: Base; }'. tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(110,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. -tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts(111,12): error TS2365: Operator '!==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. -==== tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts (64 errors) ==== +==== tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithNoRelationshipObjectsOnIndexSignature.ts (48 errors) ==== class Base { public a: string; } @@ -100,8 +84,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~ !!! error TS2365: Operator '<' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r1a4 = a4 < b4; - ~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r1b1 = b1 < a1; ~~~~~~~ @@ -113,8 +95,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~ !!! error TS2365: Operator '<' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r1b4 = b4 < a4; - ~~~~~~~ -!!! error TS2365: Operator '<' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator > var r2a1 = a1 > b1; @@ -127,8 +107,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~ !!! error TS2365: Operator '>' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r2a4 = a4 > b4; - ~~~~~~~ -!!! error TS2365: Operator '>' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r2b1 = b1 > a1; ~~~~~~~ @@ -140,8 +118,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~ !!! error TS2365: Operator '>' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r2b4 = b4 > a4; - ~~~~~~~ -!!! error TS2365: Operator '>' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator <= var r3a1 = a1 <= b1; @@ -154,8 +130,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r3a4 = a4 <= b4; - ~~~~~~~~ -!!! error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r3b1 = b1 <= a1; ~~~~~~~~ @@ -167,8 +141,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '<=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r3b4 = b4 <= a4; - ~~~~~~~~ -!!! error TS2365: Operator '<=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator >= var r4a1 = a1 >= b1; @@ -181,8 +153,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r4a4 = a4 >= b4; - ~~~~~~~~ -!!! error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r4b1 = b1 >= a1; ~~~~~~~~ @@ -194,8 +164,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '>=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r4b4 = b4 >= a4; - ~~~~~~~~ -!!! error TS2365: Operator '>=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator == var r5a1 = a1 == b1; @@ -208,8 +176,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '==' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r5a4 = a4 == b4; - ~~~~~~~~ -!!! error TS2365: Operator '==' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r5b1 = b1 == a1; ~~~~~~~~ @@ -221,8 +187,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '==' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r5b4 = b4 == a4; - ~~~~~~~~ -!!! error TS2365: Operator '==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator != var r6a1 = a1 != b1; @@ -235,8 +199,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r6a4 = a4 != b4; - ~~~~~~~~ -!!! error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r6b1 = b1 != a1; ~~~~~~~~ @@ -248,8 +210,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~ !!! error TS2365: Operator '!=' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r6b4 = b4 != a4; - ~~~~~~~~ -!!! error TS2365: Operator '!=' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator === var r7a1 = a1 === b1; @@ -262,8 +222,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~~ !!! error TS2365: Operator '===' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r7a4 = a4 === b4; - ~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r7b1 = b1 === a1; ~~~~~~~~~ @@ -275,8 +233,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~~ !!! error TS2365: Operator '===' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. var r7b4 = b4 === a4; - ~~~~~~~~~ -!!! error TS2365: Operator '===' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. // operator !== var r8a1 = a1 !== b1; @@ -289,8 +245,6 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso ~~~~~~~~~ !!! error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: Base; }' and '{ [index: number]: C; }'. var r8a4 = a4 !== b4; - ~~~~~~~~~ -!!! error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: Derived; }' and '{ [index: string]: Base; }'. var r8b1 = b1 !== a1; ~~~~~~~~~ @@ -301,6 +255,4 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso var r8b3 = b3 !== a3; ~~~~~~~~~ !!! error TS2365: Operator '!==' cannot be applied to types '{ [index: number]: C; }' and '{ [index: number]: Base; }'. - var r8b4 = b4 !== a4; - ~~~~~~~~~ -!!! error TS2365: Operator '!==' cannot be applied to types '{ [index: string]: Base; }' and '{ [index: number]: Derived; }'. \ No newline at end of file + var r8b4 = b4 !== a4; \ No newline at end of file diff --git a/tests/baselines/reference/contextualTypingOfObjectLiterals.errors.txt b/tests/baselines/reference/contextualTypingOfObjectLiterals.errors.txt deleted file mode 100644 index ac8063a4378..00000000000 --- a/tests/baselines/reference/contextualTypingOfObjectLiterals.errors.txt +++ /dev/null @@ -1,23 +0,0 @@ -tests/cases/compiler/contextualTypingOfObjectLiterals.ts(4,1): error TS2322: Type '{ x: string; }' is not assignable to type '{ [x: string]: string; }'. - Index signature is missing in type '{ x: string; }'. -tests/cases/compiler/contextualTypingOfObjectLiterals.ts(10,3): error TS2345: Argument of type '{ x: string; }' is not assignable to parameter of type '{ [s: string]: string; }'. - Index signature is missing in type '{ x: string; }'. - - -==== tests/cases/compiler/contextualTypingOfObjectLiterals.ts (2 errors) ==== - var obj1: { [x: string]: string; }; - var obj2 = {x: ""}; - obj1 = {}; // Ok - obj1 = obj2; // Error - indexer doesn't match - ~~~~ -!!! error TS2322: Type '{ x: string; }' is not assignable to type '{ [x: string]: string; }'. -!!! error TS2322: Index signature is missing in type '{ x: string; }'. - - function f(x: { [s: string]: string }) { } - - f({}); // Ok - f(obj1); // Ok - f(obj2); // Error - indexer doesn't match - ~~~~ -!!! error TS2345: Argument of type '{ x: string; }' is not assignable to parameter of type '{ [s: string]: string; }'. -!!! error TS2345: Index signature is missing in type '{ x: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/contextualTypingOfObjectLiterals.symbols b/tests/baselines/reference/contextualTypingOfObjectLiterals.symbols new file mode 100644 index 00000000000..afa34cfe481 --- /dev/null +++ b/tests/baselines/reference/contextualTypingOfObjectLiterals.symbols @@ -0,0 +1,32 @@ +=== tests/cases/compiler/contextualTypingOfObjectLiterals.ts === +var obj1: { [x: string]: string; }; +>obj1 : Symbol(obj1, Decl(contextualTypingOfObjectLiterals.ts, 0, 3)) +>x : Symbol(x, Decl(contextualTypingOfObjectLiterals.ts, 0, 13)) + +var obj2 = {x: ""}; +>obj2 : Symbol(obj2, Decl(contextualTypingOfObjectLiterals.ts, 1, 3)) +>x : Symbol(x, Decl(contextualTypingOfObjectLiterals.ts, 1, 12)) + +obj1 = {}; // Ok +>obj1 : Symbol(obj1, Decl(contextualTypingOfObjectLiterals.ts, 0, 3)) + +obj1 = obj2; // Error - indexer doesn't match +>obj1 : Symbol(obj1, Decl(contextualTypingOfObjectLiterals.ts, 0, 3)) +>obj2 : Symbol(obj2, Decl(contextualTypingOfObjectLiterals.ts, 1, 3)) + +function f(x: { [s: string]: string }) { } +>f : Symbol(f, Decl(contextualTypingOfObjectLiterals.ts, 3, 12)) +>x : Symbol(x, Decl(contextualTypingOfObjectLiterals.ts, 5, 11)) +>s : Symbol(s, Decl(contextualTypingOfObjectLiterals.ts, 5, 17)) + +f({}); // Ok +>f : Symbol(f, Decl(contextualTypingOfObjectLiterals.ts, 3, 12)) + +f(obj1); // Ok +>f : Symbol(f, Decl(contextualTypingOfObjectLiterals.ts, 3, 12)) +>obj1 : Symbol(obj1, Decl(contextualTypingOfObjectLiterals.ts, 0, 3)) + +f(obj2); // Error - indexer doesn't match +>f : Symbol(f, Decl(contextualTypingOfObjectLiterals.ts, 3, 12)) +>obj2 : Symbol(obj2, Decl(contextualTypingOfObjectLiterals.ts, 1, 3)) + diff --git a/tests/baselines/reference/contextualTypingOfObjectLiterals.types b/tests/baselines/reference/contextualTypingOfObjectLiterals.types new file mode 100644 index 00000000000..5b3068e8029 --- /dev/null +++ b/tests/baselines/reference/contextualTypingOfObjectLiterals.types @@ -0,0 +1,41 @@ +=== tests/cases/compiler/contextualTypingOfObjectLiterals.ts === +var obj1: { [x: string]: string; }; +>obj1 : { [x: string]: string; } +>x : string + +var obj2 = {x: ""}; +>obj2 : { x: string; } +>{x: ""} : { x: string; } +>x : string +>"" : string + +obj1 = {}; // Ok +>obj1 = {} : { [x: string]: undefined; } +>obj1 : { [x: string]: string; } +>{} : { [x: string]: undefined; } + +obj1 = obj2; // Error - indexer doesn't match +>obj1 = obj2 : { x: string; } +>obj1 : { [x: string]: string; } +>obj2 : { x: string; } + +function f(x: { [s: string]: string }) { } +>f : (x: { [s: string]: string; }) => void +>x : { [s: string]: string; } +>s : string + +f({}); // Ok +>f({}) : void +>f : (x: { [s: string]: string; }) => void +>{} : { [x: string]: undefined; } + +f(obj1); // Ok +>f(obj1) : void +>f : (x: { [s: string]: string; }) => void +>obj1 : { [x: string]: string; } + +f(obj2); // Error - indexer doesn't match +>f(obj2) : void +>f : (x: { [s: string]: string; }) => void +>obj2 : { x: string; } + diff --git a/tests/baselines/reference/indexerAssignability.errors.txt b/tests/baselines/reference/indexerAssignability.errors.txt deleted file mode 100644 index 44c2184517e..00000000000 --- a/tests/baselines/reference/indexerAssignability.errors.txt +++ /dev/null @@ -1,28 +0,0 @@ -tests/cases/compiler/indexerAssignability.ts(5,1): error TS2322: Type '{ [n: number]: string; }' is not assignable to type '{ [s: string]: string; }'. - Index signature is missing in type '{ [n: number]: string; }'. -tests/cases/compiler/indexerAssignability.ts(6,1): error TS2322: Type '{}' is not assignable to type '{ [s: string]: string; }'. - Index signature is missing in type '{}'. -tests/cases/compiler/indexerAssignability.ts(8,1): error TS2322: Type '{}' is not assignable to type '{ [n: number]: string; }'. - Index signature is missing in type '{}'. - - -==== tests/cases/compiler/indexerAssignability.ts (3 errors) ==== - var a: { [s: string]: string; }; - var b: { [n: number]: string; }; - var c: {}; - - a = b; - ~ -!!! error TS2322: Type '{ [n: number]: string; }' is not assignable to type '{ [s: string]: string; }'. -!!! error TS2322: Index signature is missing in type '{ [n: number]: string; }'. - a = c; - ~ -!!! error TS2322: Type '{}' is not assignable to type '{ [s: string]: string; }'. -!!! error TS2322: Index signature is missing in type '{}'. - b = a; - b = c; - ~ -!!! error TS2322: Type '{}' is not assignable to type '{ [n: number]: string; }'. -!!! error TS2322: Index signature is missing in type '{}'. - c = a; - c = b; \ No newline at end of file diff --git a/tests/baselines/reference/indexerAssignability.symbols b/tests/baselines/reference/indexerAssignability.symbols new file mode 100644 index 00000000000..379a2885e2c --- /dev/null +++ b/tests/baselines/reference/indexerAssignability.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/indexerAssignability.ts === +var a: { [s: string]: string; }; +>a : Symbol(a, Decl(indexerAssignability.ts, 0, 3)) +>s : Symbol(s, Decl(indexerAssignability.ts, 0, 10)) + +var b: { [n: number]: string; }; +>b : Symbol(b, Decl(indexerAssignability.ts, 1, 3)) +>n : Symbol(n, Decl(indexerAssignability.ts, 1, 10)) + +var c: {}; +>c : Symbol(c, Decl(indexerAssignability.ts, 2, 3)) + +a = b; +>a : Symbol(a, Decl(indexerAssignability.ts, 0, 3)) +>b : Symbol(b, Decl(indexerAssignability.ts, 1, 3)) + +a = c; +>a : Symbol(a, Decl(indexerAssignability.ts, 0, 3)) +>c : Symbol(c, Decl(indexerAssignability.ts, 2, 3)) + +b = a; +>b : Symbol(b, Decl(indexerAssignability.ts, 1, 3)) +>a : Symbol(a, Decl(indexerAssignability.ts, 0, 3)) + +b = c; +>b : Symbol(b, Decl(indexerAssignability.ts, 1, 3)) +>c : Symbol(c, Decl(indexerAssignability.ts, 2, 3)) + +c = a; +>c : Symbol(c, Decl(indexerAssignability.ts, 2, 3)) +>a : Symbol(a, Decl(indexerAssignability.ts, 0, 3)) + +c = b; +>c : Symbol(c, Decl(indexerAssignability.ts, 2, 3)) +>b : Symbol(b, Decl(indexerAssignability.ts, 1, 3)) + diff --git a/tests/baselines/reference/indexerAssignability.types b/tests/baselines/reference/indexerAssignability.types new file mode 100644 index 00000000000..bd301b59224 --- /dev/null +++ b/tests/baselines/reference/indexerAssignability.types @@ -0,0 +1,42 @@ +=== tests/cases/compiler/indexerAssignability.ts === +var a: { [s: string]: string; }; +>a : { [s: string]: string; } +>s : string + +var b: { [n: number]: string; }; +>b : { [n: number]: string; } +>n : number + +var c: {}; +>c : {} + +a = b; +>a = b : { [n: number]: string; } +>a : { [s: string]: string; } +>b : { [n: number]: string; } + +a = c; +>a = c : {} +>a : { [s: string]: string; } +>c : {} + +b = a; +>b = a : { [s: string]: string; } +>b : { [n: number]: string; } +>a : { [s: string]: string; } + +b = c; +>b = c : {} +>b : { [n: number]: string; } +>c : {} + +c = a; +>c = a : { [s: string]: string; } +>c : {} +>a : { [s: string]: string; } + +c = b; +>c = b : { [n: number]: string; } +>c : {} +>b : { [n: number]: string; } + diff --git a/tests/baselines/reference/numericIndexerConstraint2.errors.txt b/tests/baselines/reference/numericIndexerConstraint2.errors.txt index 385efbcead2..5c0337acc32 100644 --- a/tests/baselines/reference/numericIndexerConstraint2.errors.txt +++ b/tests/baselines/reference/numericIndexerConstraint2.errors.txt @@ -1,5 +1,6 @@ tests/cases/compiler/numericIndexerConstraint2.ts(4,1): error TS2322: Type '{ one: number; }' is not assignable to type '{ [index: string]: Foo; }'. - Index signature is missing in type '{ one: number; }'. + Property 'one' is incompatible with index signature. + Type 'number' is not assignable to type 'Foo'. ==== tests/cases/compiler/numericIndexerConstraint2.ts (1 errors) ==== @@ -9,4 +10,5 @@ tests/cases/compiler/numericIndexerConstraint2.ts(4,1): error TS2322: Type '{ on x = a; ~ !!! error TS2322: Type '{ one: number; }' is not assignable to type '{ [index: string]: Foo; }'. -!!! error TS2322: Index signature is missing in type '{ one: number; }'. \ No newline at end of file +!!! error TS2322: Property 'one' is incompatible with index signature. +!!! error TS2322: Type 'number' is not assignable to type 'Foo'. \ No newline at end of file diff --git a/tests/baselines/reference/numericIndexerConstraint5.errors.txt b/tests/baselines/reference/numericIndexerConstraint5.errors.txt index 98acc65eeb7..cb888de08c4 100644 --- a/tests/baselines/reference/numericIndexerConstraint5.errors.txt +++ b/tests/baselines/reference/numericIndexerConstraint5.errors.txt @@ -1,5 +1,6 @@ tests/cases/compiler/numericIndexerConstraint5.ts(2,5): error TS2322: Type '{ 0: Date; name: string; }' is not assignable to type '{ [name: number]: string; }'. - Index signature is missing in type '{ 0: Date; name: string; }'. + Property '0' is incompatible with index signature. + Type 'Date' is not assignable to type 'string'. ==== tests/cases/compiler/numericIndexerConstraint5.ts (1 errors) ==== @@ -7,4 +8,5 @@ tests/cases/compiler/numericIndexerConstraint5.ts(2,5): error TS2322: Type '{ 0: var z: { [name: number]: string } = x; ~ !!! error TS2322: Type '{ 0: Date; name: string; }' is not assignable to type '{ [name: number]: string; }'. -!!! error TS2322: Index signature is missing in type '{ 0: Date; name: string; }'. \ No newline at end of file +!!! error TS2322: Property '0' is incompatible with index signature. +!!! error TS2322: Type 'Date' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/stringIndexerAssignments1.errors.txt b/tests/baselines/reference/stringIndexerAssignments1.errors.txt index 908371bc7dd..ac99b239779 100644 --- a/tests/baselines/reference/stringIndexerAssignments1.errors.txt +++ b/tests/baselines/reference/stringIndexerAssignments1.errors.txt @@ -1,18 +1,13 @@ -tests/cases/compiler/stringIndexerAssignments1.ts(4,1): error TS2322: Type '{ one: string; }' is not assignable to type '{ [index: string]: string; one: string; }'. - Index signature is missing in type '{ one: string; }'. tests/cases/compiler/stringIndexerAssignments1.ts(5,1): error TS2322: Type '{ one: number; two: string; }' is not assignable to type '{ [index: string]: string; one: string; }'. Types of property 'one' are incompatible. Type 'number' is not assignable to type 'string'. -==== tests/cases/compiler/stringIndexerAssignments1.ts (2 errors) ==== +==== tests/cases/compiler/stringIndexerAssignments1.ts (1 errors) ==== var x: { [index: string]: string; one: string; }; var a: { one: string; }; var b: { one: number; two: string; }; x = a; - ~ -!!! error TS2322: Type '{ one: string; }' is not assignable to type '{ [index: string]: string; one: string; }'. -!!! error TS2322: Index signature is missing in type '{ one: string; }'. x = b; // error ~ !!! error TS2322: Type '{ one: number; two: string; }' is not assignable to type '{ [index: string]: string; one: string; }'. From 040effd603cf870eaef49d912403a597a7714950 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 9 Feb 2016 15:35:55 -0800 Subject: [PATCH 13/55] Include index signatures in object literal types only when computed properties are present --- src/compiler/checker.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2f236ed6e44..e6dec1d45ad 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8288,6 +8288,7 @@ namespace ts { const contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression); let typeFlags: TypeFlags = 0; + let hasDynamicProperties = false; let patternWithComputedProperties = false; for (const memberDecl of node.properties) { @@ -8353,7 +8354,10 @@ namespace ts { checkAccessorDeclaration(memberDecl); } - if (!hasDynamicName(memberDecl)) { + if (hasDynamicName(memberDecl)) { + hasDynamicProperties = true; + } + else { propertiesTable[member.name] = member; } propertiesArray.push(member); @@ -8385,7 +8389,7 @@ namespace ts { return result; function getIndexInfo(kind: IndexKind) { - if (contextualType && contextualTypeHasIndexSignature(contextualType, kind)) { + if (hasDynamicProperties && contextualType && contextualTypeHasIndexSignature(contextualType, kind)) { const propTypes: Type[] = []; for (let i = 0; i < propertiesArray.length; i++) { const propertyDecl = node.properties[i]; From d9fc61b32fb7479f5b10cca2a3efa943dadae24e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 9 Feb 2016 15:36:40 -0800 Subject: [PATCH 14/55] Accepting new baselines --- .../reference/badOverloadError.types | 2 +- .../bestCommonTypeWithContextualTyping.types | 2 +- ...ndAdditionAssignmentLHSCanBeAssigned.types | 2 +- .../reference/constEnumPropertyAccess1.types | 2 +- .../reference/contextualTypeAny.errors.txt | 15 ++++++++++++++ .../reference/contextualTypeAny.symbols | 15 -------------- .../reference/contextualTypeAny.types | 19 ------------------ .../contextualTypeArrayReturnType.types | 10 +++++----- ...tualTypeWithUnionTypeIndexSignatures.types | 20 +++++++++---------- .../contextualTypingOfObjectLiterals.types | 6 +++--- .../declarationsAndAssignments.errors.txt | 8 ++++---- ...bjectBindingPatternAndAssignment1ES5.types | 6 +++--- ...bjectBindingPatternAndAssignment1ES6.types | 6 +++--- ...ndingClassFromAliasAndUsageInIndexer.types | 2 +- .../genericBaseClassLiteralProperty2.types | 4 ++-- .../genericCallWithTupleType.errors.txt | 8 ++++---- ...nericWithIndexerOfTypeParameterType1.types | 2 +- .../indexSignaturesInferentialTyping.types | 12 +++++------ tests/baselines/reference/indexer.types | 2 +- tests/baselines/reference/indexer2.types | 2 +- tests/baselines/reference/indexer3.types | 2 +- tests/baselines/reference/indexerA.types | 2 +- ...nferentialTypingObjectLiteralMethod2.types | 4 ++-- .../inferentialTypingUsingApparentType3.types | 12 +++++------ .../reference/interfaceContextualType.types | 8 ++++---- .../reference/noErrorsInCallback.errors.txt | 8 ++++---- .../noImplicitAnyIndexingSuppressed.types | 2 +- ...rConstrainsPropertyDeclarations.errors.txt | 14 ++++++------- ...ConstrainsPropertyDeclarations2.errors.txt | 14 ++++++------- .../reference/numericIndexerConstraint4.types | 2 +- .../reference/numericIndexingResults.types | 4 ++-- tests/baselines/reference/objectIndexer.types | 4 ++-- .../objectLiteralIndexerErrors.errors.txt | 19 ++++++++++++------ .../objectLiteralIndexerNoImplicitAny.types | 2 +- .../reference/objectLiteralIndexers.types | 10 +++++----- ...ectTypesIdentityWithNumericIndexers1.types | 2 +- ...ectTypesIdentityWithNumericIndexers2.types | 2 +- ...ectTypesIdentityWithNumericIndexers3.types | 2 +- ...bjectTypesIdentityWithStringIndexers.types | 2 +- ...jectTypesIdentityWithStringIndexers2.types | 2 +- .../operatorsAndIntersectionTypes.types | 4 ++-- .../restElementWithNullInitializer.errors.txt | 4 ++-- ...rConstrainsPropertyDeclarations.errors.txt | 14 ++++++------- ...ConstrainsPropertyDeclarations2.errors.txt | 8 ++++---- .../reference/stringIndexingResults.types | 2 +- ...tringsArrayTypeDefinedInES5Mode.errors.txt | 8 ++++---- ...ringsArrayTypeNotDefinedES5Mode.errors.txt | 8 ++++---- ...ingsArrayTypeRedefinedInES6Mode.errors.txt | 8 ++++---- .../typeParameterFixingWithConstraints.types | 12 +++++------ .../baselines/reference/underscoreTest1.types | 6 +++--- .../reference/widenedTypes.errors.txt | 8 ++++---- ...wrappedAndRecursiveConstraints4.errors.txt | 4 ++-- 52 files changed, 165 insertions(+), 183 deletions(-) create mode 100644 tests/baselines/reference/contextualTypeAny.errors.txt delete mode 100644 tests/baselines/reference/contextualTypeAny.symbols delete mode 100644 tests/baselines/reference/contextualTypeAny.types diff --git a/tests/baselines/reference/badOverloadError.types b/tests/baselines/reference/badOverloadError.types index b8ae583a73c..198582af2db 100644 --- a/tests/baselines/reference/badOverloadError.types +++ b/tests/baselines/reference/badOverloadError.types @@ -6,6 +6,6 @@ function method() { >dictionary : { [index: string]: string; } ><{ [index: string]: string; }>{} : { [index: string]: string; } >index : string ->{} : { [x: string]: undefined; } +>{} : {} } diff --git a/tests/baselines/reference/bestCommonTypeWithContextualTyping.types b/tests/baselines/reference/bestCommonTypeWithContextualTyping.types index 628fe82012a..5baca43b752 100644 --- a/tests/baselines/reference/bestCommonTypeWithContextualTyping.types +++ b/tests/baselines/reference/bestCommonTypeWithContextualTyping.types @@ -36,7 +36,7 @@ var obj: { [s: string]: Contextual } = { s: e }; // { s: Ellement; [s: string]: >obj : { [s: string]: Contextual; } >s : string >Contextual : Contextual ->{ s: e } : { [x: string]: Ellement; s: Ellement; } +>{ s: e } : { s: Ellement; } >s : Ellement >e : Ellement diff --git a/tests/baselines/reference/compoundAdditionAssignmentLHSCanBeAssigned.types b/tests/baselines/reference/compoundAdditionAssignmentLHSCanBeAssigned.types index 92835611c6d..e40422450b4 100644 --- a/tests/baselines/reference/compoundAdditionAssignmentLHSCanBeAssigned.types +++ b/tests/baselines/reference/compoundAdditionAssignmentLHSCanBeAssigned.types @@ -98,7 +98,7 @@ x2 += E.a; x2 += {}; >x2 += {} : string >x2 : string ->{} : { [x: number]: undefined; } +>{} : {} x2 += null; >x2 += null : string diff --git a/tests/baselines/reference/constEnumPropertyAccess1.types b/tests/baselines/reference/constEnumPropertyAccess1.types index 5ed2b932bd0..f0e00fe7717 100644 --- a/tests/baselines/reference/constEnumPropertyAccess1.types +++ b/tests/baselines/reference/constEnumPropertyAccess1.types @@ -35,7 +35,7 @@ var o: { >idx : number } = { ->{ 1: true } : { [x: number]: boolean; 1: boolean; } +>{ 1: true } : { 1: boolean; } 1: true >true : boolean diff --git a/tests/baselines/reference/contextualTypeAny.errors.txt b/tests/baselines/reference/contextualTypeAny.errors.txt new file mode 100644 index 00000000000..09970be986d --- /dev/null +++ b/tests/baselines/reference/contextualTypeAny.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/contextualTypeAny.ts(3,5): error TS2322: Type '{ p: string; q: any; }' is not assignable to type '{ [s: string]: number; }'. + Property 'p' is incompatible with index signature. + Type 'string' is not assignable to type 'number'. + + +==== tests/cases/compiler/contextualTypeAny.ts (1 errors) ==== + var x: any; + + var obj: { [s: string]: number } = { p: "", q: x }; + ~~~ +!!! error TS2322: Type '{ p: string; q: any; }' is not assignable to type '{ [s: string]: number; }'. +!!! error TS2322: Property 'p' is incompatible with index signature. +!!! error TS2322: Type 'string' is not assignable to type 'number'. + + var arr: number[] = ["", x]; \ No newline at end of file diff --git a/tests/baselines/reference/contextualTypeAny.symbols b/tests/baselines/reference/contextualTypeAny.symbols deleted file mode 100644 index 5132c948a2e..00000000000 --- a/tests/baselines/reference/contextualTypeAny.symbols +++ /dev/null @@ -1,15 +0,0 @@ -=== tests/cases/compiler/contextualTypeAny.ts === -var x: any; ->x : Symbol(x, Decl(contextualTypeAny.ts, 0, 3)) - -var obj: { [s: string]: number } = { p: "", q: x }; ->obj : Symbol(obj, Decl(contextualTypeAny.ts, 2, 3)) ->s : Symbol(s, Decl(contextualTypeAny.ts, 2, 12)) ->p : Symbol(p, Decl(contextualTypeAny.ts, 2, 36)) ->q : Symbol(q, Decl(contextualTypeAny.ts, 2, 43)) ->x : Symbol(x, Decl(contextualTypeAny.ts, 0, 3)) - -var arr: number[] = ["", x]; ->arr : Symbol(arr, Decl(contextualTypeAny.ts, 4, 3)) ->x : Symbol(x, Decl(contextualTypeAny.ts, 0, 3)) - diff --git a/tests/baselines/reference/contextualTypeAny.types b/tests/baselines/reference/contextualTypeAny.types deleted file mode 100644 index cace92d6567..00000000000 --- a/tests/baselines/reference/contextualTypeAny.types +++ /dev/null @@ -1,19 +0,0 @@ -=== tests/cases/compiler/contextualTypeAny.ts === -var x: any; ->x : any - -var obj: { [s: string]: number } = { p: "", q: x }; ->obj : { [s: string]: number; } ->s : string ->{ p: "", q: x } : { [x: string]: any; p: string; q: any; } ->p : string ->"" : string ->q : any ->x : any - -var arr: number[] = ["", x]; ->arr : number[] ->["", x] : any[] ->"" : string ->x : any - diff --git a/tests/baselines/reference/contextualTypeArrayReturnType.types b/tests/baselines/reference/contextualTypeArrayReturnType.types index f270b33f568..6c91fd7b8b9 100644 --- a/tests/baselines/reference/contextualTypeArrayReturnType.types +++ b/tests/baselines/reference/contextualTypeArrayReturnType.types @@ -26,18 +26,18 @@ interface Transform3D { var style: IBookStyle = { >style : IBookStyle >IBookStyle : IBookStyle ->{ initialLeftPageTransforms: (width: number) => { return [ {'ry': null } ]; }} : { initialLeftPageTransforms: (width: number) => { [x: string]: any; 'ry': any; }[]; } +>{ initialLeftPageTransforms: (width: number) => { return [ {'ry': null } ]; }} : { initialLeftPageTransforms: (width: number) => { 'ry': any; }[]; } initialLeftPageTransforms: (width: number) => { ->initialLeftPageTransforms : (width: number) => { [x: string]: any; 'ry': any; }[] ->(width: number) => { return [ {'ry': null } ]; } : (width: number) => { [x: string]: any; 'ry': any; }[] +>initialLeftPageTransforms : (width: number) => { 'ry': any; }[] +>(width: number) => { return [ {'ry': null } ]; } : (width: number) => { 'ry': any; }[] >width : number return [ ->[ {'ry': null } ] : { [x: string]: null; 'ry': null; }[] +>[ {'ry': null } ] : { 'ry': null; }[] {'ry': null } ->{'ry': null } : { [x: string]: null; 'ry': null; } +>{'ry': null } : { 'ry': null; } >null : null ]; diff --git a/tests/baselines/reference/contextualTypeWithUnionTypeIndexSignatures.types b/tests/baselines/reference/contextualTypeWithUnionTypeIndexSignatures.types index 2f24ad08227..965e4d21d99 100644 --- a/tests/baselines/reference/contextualTypeWithUnionTypeIndexSignatures.types +++ b/tests/baselines/reference/contextualTypeWithUnionTypeIndexSignatures.types @@ -69,7 +69,7 @@ var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; >x : IWithNoStringIndexSignature | IWithStringIndexSignature1 >IWithNoStringIndexSignature : IWithNoStringIndexSignature >IWithStringIndexSignature1 : IWithStringIndexSignature1 ->{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; } +>{ z: a => a } : { z: (a: number) => number; } >z : (a: number) => number >a => a : (a: number) => number >a : number @@ -79,7 +79,7 @@ var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a >x : IWithNoStringIndexSignature | IWithStringIndexSignature1 >IWithNoStringIndexSignature : IWithNoStringIndexSignature >IWithStringIndexSignature1 : IWithStringIndexSignature1 ->{ foo: a => a } : { [x: string]: (a: any) => any; foo: (a: any) => any; } +>{ foo: a => a } : { foo: (a: any) => any; } >foo : (a: any) => any >a => a : (a: any) => any >a : any @@ -89,7 +89,7 @@ var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" >x : IWithNoStringIndexSignature | IWithStringIndexSignature1 >IWithNoStringIndexSignature : IWithNoStringIndexSignature >IWithStringIndexSignature1 : IWithStringIndexSignature1 ->{ foo: "hello" } : { [x: string]: string; foo: string; } +>{ foo: "hello" } : { foo: string; } >foo : string >"hello" : string @@ -97,7 +97,7 @@ var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.to >x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2 >IWithStringIndexSignature1 : IWithStringIndexSignature1 >IWithStringIndexSignature2 : IWithStringIndexSignature2 ->{ z: a => a.toString() } : { [x: string]: (a: number) => string; z: (a: number) => string; } +>{ z: a => a.toString() } : { z: (a: number) => string; } >z : (a: number) => string >a => a.toString() : (a: number) => string >a : number @@ -110,7 +110,7 @@ var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; >x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2 >IWithStringIndexSignature1 : IWithStringIndexSignature1 >IWithStringIndexSignature2 : IWithStringIndexSignature2 ->{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; } +>{ z: a => a } : { z: (a: number) => number; } >z : (a: number) => number >a => a : (a: number) => number >a : number @@ -124,7 +124,7 @@ var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a } >x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 >IWithNoNumberIndexSignature : IWithNoNumberIndexSignature >IWithNumberIndexSignature1 : IWithNumberIndexSignature1 ->{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; } +>{ 1: a => a } : { 1: (a: number) => number; } >a => a : (a: number) => number >a : number >a : number @@ -133,7 +133,7 @@ var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a } >x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 >IWithNoNumberIndexSignature : IWithNoNumberIndexSignature >IWithNumberIndexSignature1 : IWithNumberIndexSignature1 ->{ 0: a => a } : { [x: number]: (a: any) => any; 0: (a: any) => any; } +>{ 0: a => a } : { 0: (a: any) => any; } >a => a : (a: any) => any >a : any >a : any @@ -142,14 +142,14 @@ var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" >x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 >IWithNoNumberIndexSignature : IWithNoNumberIndexSignature >IWithNumberIndexSignature1 : IWithNumberIndexSignature1 ->{ 0: "hello" } : { [x: number]: string; 0: string; } +>{ 0: "hello" } : { 0: string; } >"hello" : string var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number >x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2 >IWithNumberIndexSignature1 : IWithNumberIndexSignature1 >IWithNumberIndexSignature2 : IWithNumberIndexSignature2 ->{ 1: a => a.toString() } : { [x: number]: (a: number) => string; 1: (a: number) => string; } +>{ 1: a => a.toString() } : { 1: (a: number) => string; } >a => a.toString() : (a: number) => string >a : number >a.toString() : string @@ -161,7 +161,7 @@ var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; >x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2 >IWithNumberIndexSignature1 : IWithNumberIndexSignature1 >IWithNumberIndexSignature2 : IWithNumberIndexSignature2 ->{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; } +>{ 1: a => a } : { 1: (a: number) => number; } >a => a : (a: number) => number >a : number >a : number diff --git a/tests/baselines/reference/contextualTypingOfObjectLiterals.types b/tests/baselines/reference/contextualTypingOfObjectLiterals.types index 5b3068e8029..a99fb2f39af 100644 --- a/tests/baselines/reference/contextualTypingOfObjectLiterals.types +++ b/tests/baselines/reference/contextualTypingOfObjectLiterals.types @@ -10,9 +10,9 @@ var obj2 = {x: ""}; >"" : string obj1 = {}; // Ok ->obj1 = {} : { [x: string]: undefined; } +>obj1 = {} : {} >obj1 : { [x: string]: string; } ->{} : { [x: string]: undefined; } +>{} : {} obj1 = obj2; // Error - indexer doesn't match >obj1 = obj2 : { x: string; } @@ -27,7 +27,7 @@ function f(x: { [s: string]: string }) { } f({}); // Ok >f({}) : void >f : (x: { [s: string]: string; }) => void ->{} : { [x: string]: undefined; } +>{} : {} f(obj1); // Ok >f(obj1) : void diff --git a/tests/baselines/reference/declarationsAndAssignments.errors.txt b/tests/baselines/reference/declarationsAndAssignments.errors.txt index 7c4671022ea..517eaafe4ab 100644 --- a/tests/baselines/reference/declarationsAndAssignments.errors.txt +++ b/tests/baselines/reference/declarationsAndAssignments.errors.txt @@ -11,8 +11,8 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(62,13): tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(62,16): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,13): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,16): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. -tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(67,9): error TS2461: Type '{ [x: number]: undefined; }' is not an array type. -tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(68,9): error TS2461: Type '{ [x: number]: number; 0: number; 1: number; }' is not an array type. +tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(67,9): error TS2461: Type '{}' is not an array type. +tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(68,9): error TS2461: Type '{ 0: number; 1: number; }' is not an array type. tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,14): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,11): error TS2459: Type 'undefined[]' has no property 'a' and no string index signature. @@ -122,10 +122,10 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): function f9() { var [a, b] = {}; // Error, not array type ~~~~~~ -!!! error TS2461: Type '{ [x: number]: undefined; }' is not an array type. +!!! error TS2461: Type '{}' is not an array type. var [c, d] = { 0: 10, 1: 20 }; // Error, not array type ~~~~~~ -!!! error TS2461: Type '{ [x: number]: number; 0: number; 1: number; }' is not an array type. +!!! error TS2461: Type '{ 0: number; 1: number; }' is not an array type. var [e, f] = [10, 20]; } diff --git a/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES5.types b/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES5.types index 476b5bee0cc..cb6052d93fd 100644 --- a/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES5.types +++ b/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES5.types @@ -69,7 +69,7 @@ function foo(): F { >F : F return { ->{ 1: true } : { [x: number]: boolean; 1: boolean; } +>{ 1: true } : { 1: boolean; } 1: true >true : boolean @@ -82,7 +82,7 @@ function bar(): F { >F : F return { ->{ 2: true } : { [x: number]: boolean; 2: boolean; } +>{ 2: true } : { 2: boolean; } 2: true >true : boolean @@ -114,7 +114,7 @@ function foo1(): F1 { >F1 : F1 return { ->{ "prop1": 2 } : { [x: string]: number; "prop1": number; } +>{ "prop1": 2 } : { "prop1": number; } "prop1": 2 >2 : number diff --git a/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES6.types b/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES6.types index 7a7f631eddf..2d77c20f65b 100644 --- a/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES6.types +++ b/tests/baselines/reference/destructuringObjectBindingPatternAndAssignment1ES6.types @@ -69,7 +69,7 @@ function foo(): F { >F : F return { ->{ 1: true } : { [x: number]: boolean; 1: boolean; } +>{ 1: true } : { 1: boolean; } 1: true >true : boolean @@ -82,7 +82,7 @@ function bar(): F { >F : F return { ->{ 2: true } : { [x: number]: boolean; 2: boolean; } +>{ 2: true } : { 2: boolean; } 2: true >true : boolean @@ -114,7 +114,7 @@ function foo1(): F1 { >F1 : F1 return { ->{ "prop1": 2 } : { [x: string]: number; "prop1": number; } +>{ "prop1": 2 } : { "prop1": number; } "prop1": 2 >2 : number diff --git a/tests/baselines/reference/extendingClassFromAliasAndUsageInIndexer.types b/tests/baselines/reference/extendingClassFromAliasAndUsageInIndexer.types index e1bfd195698..9825a75a220 100644 --- a/tests/baselines/reference/extendingClassFromAliasAndUsageInIndexer.types +++ b/tests/baselines/reference/extendingClassFromAliasAndUsageInIndexer.types @@ -26,7 +26,7 @@ var moduleMap: { [key: string]: IHasVisualizationModel } = { >moduleMap : { [key: string]: IHasVisualizationModel; } >key : string >IHasVisualizationModel : IHasVisualizationModel ->{ "moduleA": moduleA, "moduleB": moduleB} : { [x: string]: typeof moduleA; "moduleA": typeof moduleA; "moduleB": typeof moduleB; } +>{ "moduleA": moduleA, "moduleB": moduleB} : { "moduleA": typeof moduleA; "moduleB": typeof moduleB; } "moduleA": moduleA, >moduleA : typeof moduleA diff --git a/tests/baselines/reference/genericBaseClassLiteralProperty2.types b/tests/baselines/reference/genericBaseClassLiteralProperty2.types index d7d512165f0..3e320824925 100644 --- a/tests/baselines/reference/genericBaseClassLiteralProperty2.types +++ b/tests/baselines/reference/genericBaseClassLiteralProperty2.types @@ -14,11 +14,11 @@ class BaseCollection2 { constructor() { this._itemsByKey = {}; ->this._itemsByKey = {} : { [x: string]: undefined; } +>this._itemsByKey = {} : {} >this._itemsByKey : { [key: string]: TItem; } >this : this >_itemsByKey : { [key: string]: TItem; } ->{} : { [x: string]: undefined; } +>{} : {} } } diff --git a/tests/baselines/reference/genericCallWithTupleType.errors.txt b/tests/baselines/reference/genericCallWithTupleType.errors.txt index d03f8d5e68a..bcf580320e5 100644 --- a/tests/baselines/reference/genericCallWithTupleType.errors.txt +++ b/tests/baselines/reference/genericCallWithTupleType.errors.txt @@ -9,9 +9,9 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(22,1): error TS2322: Type '[number, string]' is not assignable to type '[string, number]'. Types of property '0' are incompatible. Type 'number' is not assignable to type 'string'. -tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(23,1): error TS2322: Type '[{ [x: number]: undefined; }, {}]' is not assignable to type '[string, number]'. +tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(23,1): error TS2322: Type '[{}, {}]' is not assignable to type '[string, number]'. Types of property '0' are incompatible. - Type '{ [x: number]: undefined; }' is not assignable to type 'string'. + Type '{}' is not assignable to type 'string'. tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(24,1): error TS2322: Type '[{}]' is not assignable to type '[{}, {}]'. Property '1' is missing in type '[{}]'. @@ -55,9 +55,9 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup !!! error TS2322: Type 'number' is not assignable to type 'string'. i1.tuple1 = [{}, {}]; ~~~~~~~~~ -!!! error TS2322: Type '[{ [x: number]: undefined; }, {}]' is not assignable to type '[string, number]'. +!!! error TS2322: Type '[{}, {}]' is not assignable to type '[string, number]'. !!! error TS2322: Types of property '0' are incompatible. -!!! error TS2322: Type '{ [x: number]: undefined; }' is not assignable to type 'string'. +!!! error TS2322: Type '{}' is not assignable to type 'string'. i2.tuple1 = [{}]; ~~~~~~~~~ !!! error TS2322: Type '[{}]' is not assignable to type '[{}, {}]'. diff --git a/tests/baselines/reference/genericWithIndexerOfTypeParameterType1.types b/tests/baselines/reference/genericWithIndexerOfTypeParameterType1.types index 337993dc2a6..a316773e5c8 100644 --- a/tests/baselines/reference/genericWithIndexerOfTypeParameterType1.types +++ b/tests/baselines/reference/genericWithIndexerOfTypeParameterType1.types @@ -8,7 +8,7 @@ class LazyArray { ><{ [objectId: string]: T; }>{} : { [objectId: string]: T; } >objectId : string >T : T ->{} : { [x: string]: undefined; } +>{} : {} array() { >array : () => { [objectId: string]: T; } diff --git a/tests/baselines/reference/indexSignaturesInferentialTyping.types b/tests/baselines/reference/indexSignaturesInferentialTyping.types index 328dbc18e4f..8545a7d4ca9 100644 --- a/tests/baselines/reference/indexSignaturesInferentialTyping.types +++ b/tests/baselines/reference/indexSignaturesInferentialTyping.types @@ -21,15 +21,15 @@ var x1 = foo({ 0: 0, 1: 1 }); // type should be number >x1 : number >foo({ 0: 0, 1: 1 }) : number >foo : (items: { [index: number]: T; }) => T ->{ 0: 0, 1: 1 } : { [x: number]: number; 0: number; 1: number; } +>{ 0: 0, 1: 1 } : { 0: number; 1: number; } >0 : number >1 : number var x2 = foo({ zero: 0, one: 1 }); ->x2 : any ->foo({ zero: 0, one: 1 }) : any +>x2 : {} +>foo({ zero: 0, one: 1 }) : {} >foo : (items: { [index: number]: T; }) => T ->{ zero: 0, one: 1 } : { [x: number]: undefined; zero: number; one: number; } +>{ zero: 0, one: 1 } : { zero: number; one: number; } >zero : number >0 : number >one : number @@ -39,7 +39,7 @@ var x3 = bar({ 0: 0, 1: 1 }); >x3 : number >bar({ 0: 0, 1: 1 }) : number >bar : (items: { [index: string]: T; }) => T ->{ 0: 0, 1: 1 } : { [x: string]: number; 0: number; 1: number; } +>{ 0: 0, 1: 1 } : { 0: number; 1: number; } >0 : number >1 : number @@ -47,7 +47,7 @@ var x4 = bar({ zero: 0, one: 1 }); // type should be number >x4 : number >bar({ zero: 0, one: 1 }) : number >bar : (items: { [index: string]: T; }) => T ->{ zero: 0, one: 1 } : { [x: string]: number; zero: number; one: number; } +>{ zero: 0, one: 1 } : { zero: number; one: number; } >zero : number >0 : number >one : number diff --git a/tests/baselines/reference/indexer.types b/tests/baselines/reference/indexer.types index a0142bb9383..f24c90f6ea1 100644 --- a/tests/baselines/reference/indexer.types +++ b/tests/baselines/reference/indexer.types @@ -17,7 +17,7 @@ interface JQuery { var jq:JQuery={ 0: { id : "a" }, 1: { id : "b" } }; >jq : JQuery >JQuery : JQuery ->{ 0: { id : "a" }, 1: { id : "b" } } : { [x: number]: { id: string; }; 0: { id: string; }; 1: { id: string; }; } +>{ 0: { id : "a" }, 1: { id : "b" } } : { 0: { id: string; }; 1: { id: string; }; } >{ id : "a" } : { id: string; } >id : string >"a" : string diff --git a/tests/baselines/reference/indexer2.types b/tests/baselines/reference/indexer2.types index 7f1ccd41cb5..ff82707edd3 100644 --- a/tests/baselines/reference/indexer2.types +++ b/tests/baselines/reference/indexer2.types @@ -17,5 +17,5 @@ var directChildrenMap = {}; >directChildrenMap : IDirectChildrenMap >{} : IDirectChildrenMap >IDirectChildrenMap : IDirectChildrenMap ->{} : { [x: number]: undefined; } +>{} : {} diff --git a/tests/baselines/reference/indexer3.types b/tests/baselines/reference/indexer3.types index 8f0582f782a..28bcd9cab0c 100644 --- a/tests/baselines/reference/indexer3.types +++ b/tests/baselines/reference/indexer3.types @@ -3,7 +3,7 @@ var dateMap: { [x: string]: Date; } = {} >dateMap : { [x: string]: Date; } >x : string >Date : Date ->{} : { [x: string]: undefined; } +>{} : {} var r: Date = dateMap["hello"] // result type includes indexer using BCT >r : Date diff --git a/tests/baselines/reference/indexerA.types b/tests/baselines/reference/indexerA.types index c6ff81b3a26..ee6351eae33 100644 --- a/tests/baselines/reference/indexerA.types +++ b/tests/baselines/reference/indexerA.types @@ -17,7 +17,7 @@ class JQuery { var jq:JQuery={ 0: { id : "a" }, 1: { id : "b" } }; >jq : JQuery >JQuery : JQuery ->{ 0: { id : "a" }, 1: { id : "b" } } : { [x: number]: { id: string; }; 0: { id: string; }; 1: { id: string; }; } +>{ 0: { id : "a" }, 1: { id : "b" } } : { 0: { id: string; }; 1: { id: string; }; } >{ id : "a" } : { id: string; } >id : string >"a" : string diff --git a/tests/baselines/reference/inferentialTypingObjectLiteralMethod2.types b/tests/baselines/reference/inferentialTypingObjectLiteralMethod2.types index be937410cef..f70305739b8 100644 --- a/tests/baselines/reference/inferentialTypingObjectLiteralMethod2.types +++ b/tests/baselines/reference/inferentialTypingObjectLiteralMethod2.types @@ -30,13 +30,13 @@ foo("", { method(p1) { return p1.length } }, { method(p2) { return undefined } } >foo("", { method(p1) { return p1.length } }, { method(p2) { return undefined } }) : string >foo : (x: T, y: Int, z: Int) => T >"" : string ->{ method(p1) { return p1.length } } : { [x: string]: (p1: string) => number; method(p1: string): number; } +>{ method(p1) { return p1.length } } : { method(p1: string): number; } >method : (p1: string) => number >p1 : string >p1.length : number >p1 : string >length : number ->{ method(p2) { return undefined } } : { [x: string]: (p2: number) => any; method(p2: number): any; } +>{ method(p2) { return undefined } } : { method(p2: number): any; } >method : (p2: number) => any >p2 : number >undefined : undefined diff --git a/tests/baselines/reference/inferentialTypingUsingApparentType3.types b/tests/baselines/reference/inferentialTypingUsingApparentType3.types index 9e0a4ceaea6..e375fe981e8 100644 --- a/tests/baselines/reference/inferentialTypingUsingApparentType3.types +++ b/tests/baselines/reference/inferentialTypingUsingApparentType3.types @@ -49,10 +49,10 @@ class ObjectField }> { } var person = new ObjectField({ ->person : ObjectField<{}, { [x: string]: NumberField | CharField; id: NumberField; name: CharField; }> ->new ObjectField({ id: new NumberField(), name: new CharField()}) : ObjectField<{}, { [x: string]: NumberField | CharField; id: NumberField; name: CharField; }> +>person : ObjectField<{}, { id: NumberField; name: CharField; }> +>new ObjectField({ id: new NumberField(), name: new CharField()}) : ObjectField<{}, { id: NumberField; name: CharField; }> >ObjectField : typeof ObjectField ->{ id: new NumberField(), name: new CharField()} : { [x: string]: NumberField | CharField; id: NumberField; name: CharField; } +>{ id: new NumberField(), name: new CharField()} : { id: NumberField; name: CharField; } id: new NumberField(), >id : NumberField @@ -68,8 +68,8 @@ var person = new ObjectField({ person.fields.id; >person.fields.id : NumberField ->person.fields : { [x: string]: NumberField | CharField; id: NumberField; name: CharField; } ->person : ObjectField<{}, { [x: string]: NumberField | CharField; id: NumberField; name: CharField; }> ->fields : { [x: string]: NumberField | CharField; id: NumberField; name: CharField; } +>person.fields : { id: NumberField; name: CharField; } +>person : ObjectField<{}, { id: NumberField; name: CharField; }> +>fields : { id: NumberField; name: CharField; } >id : NumberField diff --git a/tests/baselines/reference/interfaceContextualType.types b/tests/baselines/reference/interfaceContextualType.types index e827f9a9d9b..0e835a2f1f9 100644 --- a/tests/baselines/reference/interfaceContextualType.types +++ b/tests/baselines/reference/interfaceContextualType.types @@ -27,11 +27,11 @@ class Bug { >ok : () => void this.values = {}; ->this.values = {} : { [x: string]: undefined; } +>this.values = {} : {} >this.values : IMap >this : this >values : IMap ->{} : { [x: string]: undefined; } +>{} : {} this.values['comments'] = { italic: true }; >this.values['comments'] = { italic: true } : { italic: boolean; } @@ -48,11 +48,11 @@ class Bug { >shouldBeOK : () => void this.values = { ->this.values = { comments: { italic: true } } : { [x: string]: { italic: boolean; }; comments: { italic: boolean; }; } +>this.values = { comments: { italic: true } } : { comments: { italic: boolean; }; } >this.values : IMap >this : this >values : IMap ->{ comments: { italic: true } } : { [x: string]: { italic: boolean; }; comments: { italic: boolean; }; } +>{ comments: { italic: true } } : { comments: { italic: boolean; }; } comments: { italic: true } >comments : { italic: boolean; } diff --git a/tests/baselines/reference/noErrorsInCallback.errors.txt b/tests/baselines/reference/noErrorsInCallback.errors.txt index 90fdbbd77a5..f0bfe00199e 100644 --- a/tests/baselines/reference/noErrorsInCallback.errors.txt +++ b/tests/baselines/reference/noErrorsInCallback.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/noErrorsInCallback.ts(4,19): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'string'. -tests/cases/compiler/noErrorsInCallback.ts(6,23): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'string'. +tests/cases/compiler/noErrorsInCallback.ts(4,19): error TS2345: Argument of type '{}' is not assignable to parameter of type 'string'. +tests/cases/compiler/noErrorsInCallback.ts(6,23): error TS2345: Argument of type '{}' is not assignable to parameter of type 'string'. ==== tests/cases/compiler/noErrorsInCallback.ts (2 errors) ==== @@ -8,10 +8,10 @@ tests/cases/compiler/noErrorsInCallback.ts(6,23): error TS2345: Argument of type } var one = new Bar({}); // Error ~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'string'. +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'string'. [].forEach(() => { var two = new Bar({}); // No error? ~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'string'. +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'string'. }); \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyIndexingSuppressed.types b/tests/baselines/reference/noImplicitAnyIndexingSuppressed.types index b7b21bf8f92..d82b37e0f9e 100644 --- a/tests/baselines/reference/noImplicitAnyIndexingSuppressed.types +++ b/tests/baselines/reference/noImplicitAnyIndexingSuppressed.types @@ -87,7 +87,7 @@ interface MyMap { var m: MyMap = { >m : MyMap >MyMap : MyMap ->{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { [x: string]: number; "0": number; "1": number; "2": number; "Okay that's enough for today.": number; } +>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { "0": number; "1": number; "2": number; "Okay that's enough for today.": number; } "0": 0, >0 : number diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt index 35673eb3072..bb01b2fe125 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt @@ -5,10 +5,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(36,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(50,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo(): string; }' is not assignable to type '{ [x: number]: string; }'. - Index signatures are incompatible. - Type 'string | number' is not assignable to type 'string'. - Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo(): string; }' is not assignable to type '{ [x: number]: string; }'. + Property '2.0' is incompatible with index signature. + Type 'number' is not assignable to type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(88,9): error TS2304: Cannot find name 'Myn'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(90,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(93,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. @@ -108,10 +107,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo // error var b: { [x: number]: string; } = { ~ -!!! error TS2322: Type '{ [x: number]: string | number; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo(): string; }' is not assignable to type '{ [x: number]: string; }'. -!!! error TS2322: Index signatures are incompatible. -!!! error TS2322: Type 'string | number' is not assignable to type 'string'. -!!! error TS2322: Type 'number' is not assignable to type 'string'. +!!! error TS2322: Type '{ 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo(): string; }' is not assignable to type '{ [x: number]: string; }'. +!!! error TS2322: Property '2.0' is incompatible with index signature. +!!! error TS2322: Type 'number' is not assignable to type 'string'. a: '', b: 1, c: () => { }, diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt index ff18160e9a5..332b356c732 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt @@ -1,10 +1,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(16,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(25,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(34,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(39,5): error TS2322: Type '{ [x: number]: A | number; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }'. - Index signatures are incompatible. - Type 'A | number' is not assignable to type 'A'. - Type 'number' is not assignable to type 'A'. +tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(39,5): error TS2322: Type '{ 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }'. + Property '3.0' is incompatible with index signature. + Type 'number' is not assignable to type 'A'. ==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts (4 errors) ==== @@ -54,10 +53,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo // error var b: { [x: number]: A } = { ~ -!!! error TS2322: Type '{ [x: number]: A | number; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }'. -!!! error TS2322: Index signatures are incompatible. -!!! error TS2322: Type 'A | number' is not assignable to type 'A'. -!!! error TS2322: Type 'number' is not assignable to type 'A'. +!!! error TS2322: Type '{ 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }'. +!!! error TS2322: Property '3.0' is incompatible with index signature. +!!! error TS2322: Type 'number' is not assignable to type 'A'. 1.0: new A(), 2.0: new B(), "2.5": new B(), diff --git a/tests/baselines/reference/numericIndexerConstraint4.types b/tests/baselines/reference/numericIndexerConstraint4.types index 97e72b5088d..c8af4d54a9a 100644 --- a/tests/baselines/reference/numericIndexerConstraint4.types +++ b/tests/baselines/reference/numericIndexerConstraint4.types @@ -22,7 +22,7 @@ var x: { >A : A } = { data: new B() } ->{ data: new B() } : { [x: number]: undefined; data: B; } +>{ data: new B() } : { data: B; } >data : B >new B() : B >B : typeof B diff --git a/tests/baselines/reference/numericIndexingResults.types b/tests/baselines/reference/numericIndexingResults.types index 560bbdc74a8..9a060b388e3 100644 --- a/tests/baselines/reference/numericIndexingResults.types +++ b/tests/baselines/reference/numericIndexingResults.types @@ -151,7 +151,7 @@ var r6 = a[3]; var b: { [x: number]: string } = { 1: '', "2": '' } >b : { [x: number]: string; } >x : number ->{ 1: '', "2": '' } : { [x: number]: string; 1: string; "2": string; } +>{ 1: '', "2": '' } : { 1: string; "2": string; } >'' : string >'' : string @@ -194,7 +194,7 @@ var r6 = b[3]; var b2: { [x: number]: string; 1: string; "2": string; } = { 1: '', "2": '' } >b2 : { [x: number]: string; 1: string; "2": string; } >x : number ->{ 1: '', "2": '' } : { [x: number]: string; 1: string; "2": string; } +>{ 1: '', "2": '' } : { 1: string; "2": string; } >'' : string >'' : string diff --git a/tests/baselines/reference/objectIndexer.types b/tests/baselines/reference/objectIndexer.types index 3dcd7bcb77e..b11c475a386 100644 --- a/tests/baselines/reference/objectIndexer.types +++ b/tests/baselines/reference/objectIndexer.types @@ -23,11 +23,11 @@ class Emitter { constructor () { this.listeners = {}; ->this.listeners = {} : { [x: string]: undefined; } +>this.listeners = {} : {} >this.listeners : IMap >this : this >listeners : IMap ->{} : { [x: string]: undefined; } +>{} : {} } } diff --git a/tests/baselines/reference/objectLiteralIndexerErrors.errors.txt b/tests/baselines/reference/objectLiteralIndexerErrors.errors.txt index f825c416311..9d5ff7ea0a2 100644 --- a/tests/baselines/reference/objectLiteralIndexerErrors.errors.txt +++ b/tests/baselines/reference/objectLiteralIndexerErrors.errors.txt @@ -1,10 +1,13 @@ -tests/cases/compiler/objectLiteralIndexerErrors.ts(13,5): error TS2322: Type '{ [x: string]: A; [x: number]: A; 0: A; x: B; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. - Index signatures are incompatible. +tests/cases/compiler/objectLiteralIndexerErrors.ts(13,5): error TS2322: Type '{ 0: A; x: B; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. + Property '0' is incompatible with index signature. Type 'A' is not assignable to type 'B'. Property 'y' is missing in type 'A'. +tests/cases/compiler/objectLiteralIndexerErrors.ts(14,1): error TS2322: Type '{ 0: A; x: any; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. + Property '0' is incompatible with index signature. + Type 'A' is not assignable to type 'B'. -==== tests/cases/compiler/objectLiteralIndexerErrors.ts (1 errors) ==== +==== tests/cases/compiler/objectLiteralIndexerErrors.ts (2 errors) ==== interface A { x: number; } @@ -19,8 +22,12 @@ tests/cases/compiler/objectLiteralIndexerErrors.ts(13,5): error TS2322: Type '{ var o1: { [s: string]: A;[n: number]: B; } = { x: b, 0: a }; // both indexers are A ~~ -!!! error TS2322: Type '{ [x: string]: A; [x: number]: A; 0: A; x: B; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. -!!! error TS2322: Index signatures are incompatible. +!!! error TS2322: Type '{ 0: A; x: B; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. +!!! error TS2322: Property '0' is incompatible with index signature. !!! error TS2322: Type 'A' is not assignable to type 'B'. !!! error TS2322: Property 'y' is missing in type 'A'. - o1 = { x: c, 0: a }; // string indexer is any, number indexer is A \ No newline at end of file + o1 = { x: c, 0: a }; // string indexer is any, number indexer is A + ~~ +!!! error TS2322: Type '{ 0: A; x: any; }' is not assignable to type '{ [s: string]: A; [n: number]: B; }'. +!!! error TS2322: Property '0' is incompatible with index signature. +!!! error TS2322: Type 'A' is not assignable to type 'B'. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralIndexerNoImplicitAny.types b/tests/baselines/reference/objectLiteralIndexerNoImplicitAny.types index a49f76d02e9..b0ddd9f60d0 100644 --- a/tests/baselines/reference/objectLiteralIndexerNoImplicitAny.types +++ b/tests/baselines/reference/objectLiteralIndexerNoImplicitAny.types @@ -9,7 +9,7 @@ interface I { var x: I = { >x : I >I : I ->{ p: null} : { [x: string]: null; p: null; } +>{ p: null} : { p: null; } p: null >p : null diff --git a/tests/baselines/reference/objectLiteralIndexers.types b/tests/baselines/reference/objectLiteralIndexers.types index d5add00ebaf..9e3aa4bac49 100644 --- a/tests/baselines/reference/objectLiteralIndexers.types +++ b/tests/baselines/reference/objectLiteralIndexers.types @@ -31,23 +31,23 @@ var o1: { [s: string]: A;[n: number]: B; } = { x: a, 0: b }; // string indexer i >A : A >n : number >B : B ->{ x: a, 0: b } : { [x: string]: A; [x: number]: B; 0: B; x: A; } +>{ x: a, 0: b } : { 0: B; x: A; } >x : A >a : A >b : B o1 = { x: b, 0: c }; // both indexers are any ->o1 = { x: b, 0: c } : { [x: string]: any; [x: number]: any; 0: any; x: B; } +>o1 = { x: b, 0: c } : { 0: any; x: B; } >o1 : { [s: string]: A; [n: number]: B; } ->{ x: b, 0: c } : { [x: string]: any; [x: number]: any; 0: any; x: B; } +>{ x: b, 0: c } : { 0: any; x: B; } >x : B >b : B >c : any o1 = { x: c, 0: b }; // string indexer is any, number indexer is B ->o1 = { x: c, 0: b } : { [x: string]: any; [x: number]: B; 0: B; x: any; } +>o1 = { x: c, 0: b } : { 0: B; x: any; } >o1 : { [s: string]: A; [n: number]: B; } ->{ x: c, 0: b } : { [x: string]: any; [x: number]: B; 0: B; x: any; } +>{ x: c, 0: b } : { 0: B; x: any; } >x : any >c : any >b : B diff --git a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers1.types b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers1.types index 2284d06ffbd..c5de5c7b9b4 100644 --- a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers1.types +++ b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers1.types @@ -50,7 +50,7 @@ var a: { var b: { [x: number]: string; } = { foo: '' }; >b : { [x: number]: string; } >x : number ->{ foo: '' } : { [x: number]: undefined; foo: string; } +>{ foo: '' } : { foo: string; } >foo : string >'' : string diff --git a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers2.types b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers2.types index 1a580297b1b..17fc6a07a00 100644 --- a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers2.types +++ b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers2.types @@ -64,7 +64,7 @@ var b: { [x: number]: Derived; } = { foo: null }; >b : { [x: number]: Derived; } >x : number >Derived : Derived ->{ foo: null } : { [x: number]: undefined; foo: Derived; } +>{ foo: null } : { foo: Derived; } >foo : Derived >null : Derived >Derived : Derived diff --git a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers3.types b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers3.types index ae384bbb4db..99e6b8c8632 100644 --- a/tests/baselines/reference/objectTypesIdentityWithNumericIndexers3.types +++ b/tests/baselines/reference/objectTypesIdentityWithNumericIndexers3.types @@ -50,7 +50,7 @@ var a: { var b: { [x: number]: string; } = { foo: '' }; >b : { [x: number]: string; } >x : number ->{ foo: '' } : { [x: number]: undefined; foo: string; } +>{ foo: '' } : { foo: string; } >foo : string >'' : string diff --git a/tests/baselines/reference/objectTypesIdentityWithStringIndexers.types b/tests/baselines/reference/objectTypesIdentityWithStringIndexers.types index a7eeb672504..8393eaf68a0 100644 --- a/tests/baselines/reference/objectTypesIdentityWithStringIndexers.types +++ b/tests/baselines/reference/objectTypesIdentityWithStringIndexers.types @@ -50,7 +50,7 @@ var a: { var b: { [x: string]: string; } = { foo: '' }; >b : { [x: string]: string; } >x : string ->{ foo: '' } : { [x: string]: string; foo: string; } +>{ foo: '' } : { foo: string; } >foo : string >'' : string diff --git a/tests/baselines/reference/objectTypesIdentityWithStringIndexers2.types b/tests/baselines/reference/objectTypesIdentityWithStringIndexers2.types index d1394252117..814d0c91814 100644 --- a/tests/baselines/reference/objectTypesIdentityWithStringIndexers2.types +++ b/tests/baselines/reference/objectTypesIdentityWithStringIndexers2.types @@ -64,7 +64,7 @@ var b: { [x: string]: Derived; } = { foo: null }; >b : { [x: string]: Derived; } >x : string >Derived : Derived ->{ foo: null } : { [x: string]: Derived; foo: Derived; } +>{ foo: null } : { foo: Derived; } >foo : Derived >null : Derived >Derived : Derived diff --git a/tests/baselines/reference/operatorsAndIntersectionTypes.types b/tests/baselines/reference/operatorsAndIntersectionTypes.types index 70611776fc1..6b5987d3a69 100644 --- a/tests/baselines/reference/operatorsAndIntersectionTypes.types +++ b/tests/baselines/reference/operatorsAndIntersectionTypes.types @@ -28,7 +28,7 @@ function createSerialNo() { let map1: { [x: string]: number } = {}; >map1 : { [x: string]: number; } >x : string ->{} : { [x: string]: undefined; } +>{} : {} let guid = createGuid(); >guid : string & { $Guid: any; } @@ -45,7 +45,7 @@ map1[guid] = 123; // Can with tagged string let map2: { [x: number]: string } = {}; >map2 : { [x: number]: string; } >x : number ->{} : { [x: number]: undefined; } +>{} : {} let serialNo = createSerialNo(); >serialNo : number & { $SerialNo: any; } diff --git a/tests/baselines/reference/restElementWithNullInitializer.errors.txt b/tests/baselines/reference/restElementWithNullInitializer.errors.txt index 7beb7b9a642..4658acc5ff4 100644 --- a/tests/baselines/reference/restElementWithNullInitializer.errors.txt +++ b/tests/baselines/reference/restElementWithNullInitializer.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(1,15): error TS2461: Type 'null' is not an array type. tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(4,15): error TS2461: Type 'undefined' is not an array type. -tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(7,15): error TS2461: Type '{ [x: number]: undefined; }' is not an array type. +tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(7,15): error TS2461: Type '{}' is not an array type. ==== tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts (3 errors) ==== @@ -16,7 +16,7 @@ tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(7,15 function foo3([...r] = {}) { ~~~~~~ -!!! error TS2461: Type '{ [x: number]: undefined; }' is not an array type. +!!! error TS2461: Type '{}' is not an array type. } function foo4([...r] = []) { diff --git a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt index 08881be2a86..8f4bbe2025a 100644 --- a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt @@ -22,10 +22,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(71,5): error TS2411: Property 'foo' of type '() => string' is not assignable to string index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(73,5): error TS2411: Property '"4.0"' of type 'number' is not assignable to string index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(74,5): error TS2411: Property 'f' of type 'MyString' is not assignable to string index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: string]: string | number | (() => void) | MyString; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo(): string; }' is not assignable to type '{ [x: string]: string; }'. - Index signatures are incompatible. - Type 'string | number | (() => void) | MyString' is not assignable to type 'string'. - Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo(): string; }' is not assignable to type '{ [x: string]: string; }'. + Property '2.0' is incompatible with index signature. + Type 'number' is not assignable to type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(90,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts(93,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. @@ -158,10 +157,9 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon // error var b: { [x: string]: string; } = { ~ -!!! error TS2322: Type '{ [x: string]: string | number | (() => void) | MyString; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo(): string; }' is not assignable to type '{ [x: string]: string; }'. -!!! error TS2322: Index signatures are incompatible. -!!! error TS2322: Type 'string | number | (() => void) | MyString' is not assignable to type 'string'. -!!! error TS2322: Type 'number' is not assignable to type 'string'. +!!! error TS2322: Type '{ 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo(): string; }' is not assignable to type '{ [x: string]: string; }'. +!!! error TS2322: Property '2.0' is incompatible with index signature. +!!! error TS2322: Type 'number' is not assignable to type 'string'. a: '', b: 1, c: () => { }, diff --git a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations2.errors.txt b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations2.errors.txt index faef7905453..dacfb1a522d 100644 --- a/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations2.errors.txt +++ b/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations2.errors.txt @@ -4,8 +4,8 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(24,5): error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(31,5): error TS2411: Property 'c' of type 'number' is not assignable to string index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(32,5): error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(36,5): error TS2322: Type '{ [x: string]: typeof A; a: typeof A; b: typeof B; }' is not assignable to type '{ [x: string]: A; }'. - Index signatures are incompatible. +tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(36,5): error TS2322: Type '{ a: typeof A; b: typeof B; }' is not assignable to type '{ [x: string]: A; }'. + Property 'a' is incompatible with index signature. Type 'typeof A' is not assignable to type 'A'. Property 'foo' is missing in type 'typeof A'. @@ -60,8 +60,8 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon // error var b: { [x: string]: A } = { ~ -!!! error TS2322: Type '{ [x: string]: typeof A; a: typeof A; b: typeof B; }' is not assignable to type '{ [x: string]: A; }'. -!!! error TS2322: Index signatures are incompatible. +!!! error TS2322: Type '{ a: typeof A; b: typeof B; }' is not assignable to type '{ [x: string]: A; }'. +!!! error TS2322: Property 'a' is incompatible with index signature. !!! error TS2322: Type 'typeof A' is not assignable to type 'A'. !!! error TS2322: Property 'foo' is missing in type 'typeof A'. a: A, diff --git a/tests/baselines/reference/stringIndexingResults.types b/tests/baselines/reference/stringIndexingResults.types index 9f613312e5a..1fdc580b1a2 100644 --- a/tests/baselines/reference/stringIndexingResults.types +++ b/tests/baselines/reference/stringIndexingResults.types @@ -95,7 +95,7 @@ var r9 = a[1]; var b: { [x: string]: string } = { y: '' } >b : { [x: string]: string; } >x : string ->{ y: '' } : { [x: string]: string; y: string; } +>{ y: '' } : { y: string; } >y : string >'' : string diff --git a/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt b/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt index e6910840d39..94ab5028647 100644 --- a/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt +++ b/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(8,3): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. - Property 'raw' is missing in type '{ [x: number]: undefined; }'. +tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(8,3): error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. + Property 'raw' is missing in type '{}'. ==== tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts (1 errors) ==== @@ -12,7 +12,7 @@ tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(8,3): error TS2 f({}, 10, 10); ~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. -!!! error TS2345: Property 'raw' is missing in type '{ [x: number]: undefined; }'. +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. +!!! error TS2345: Property 'raw' is missing in type '{}'. f `abcdef${ 1234 }${ 5678 }ghijkl`; \ No newline at end of file diff --git a/tests/baselines/reference/templateStringsArrayTypeNotDefinedES5Mode.errors.txt b/tests/baselines/reference/templateStringsArrayTypeNotDefinedES5Mode.errors.txt index e8bf38c1048..1201c82f4d0 100644 --- a/tests/baselines/reference/templateStringsArrayTypeNotDefinedES5Mode.errors.txt +++ b/tests/baselines/reference/templateStringsArrayTypeNotDefinedES5Mode.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/templateStringsArrayTypeNotDefinedES5Mode.ts(5,3): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. - Property 'raw' is missing in type '{ [x: number]: undefined; }'. +tests/cases/compiler/templateStringsArrayTypeNotDefinedES5Mode.ts(5,3): error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. + Property 'raw' is missing in type '{}'. ==== tests/cases/compiler/templateStringsArrayTypeNotDefinedES5Mode.ts (1 errors) ==== @@ -9,7 +9,7 @@ tests/cases/compiler/templateStringsArrayTypeNotDefinedES5Mode.ts(5,3): error TS f({}, 10, 10); ~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. -!!! error TS2345: Property 'raw' is missing in type '{ [x: number]: undefined; }'. +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. +!!! error TS2345: Property 'raw' is missing in type '{}'. f `abcdef${ 1234 }${ 5678 }ghijkl`; \ No newline at end of file diff --git a/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt b/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt index ac30556a235..f410d408482 100644 --- a/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt +++ b/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt @@ -1,5 +1,5 @@ -tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts(8,3): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. - Property 'raw' is missing in type '{ [x: number]: undefined; }'. +tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts(8,3): error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. + Property 'raw' is missing in type '{}'. ==== tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts (1 errors) ==== @@ -12,7 +12,7 @@ tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts(8,3): error T f({}, 10, 10); ~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'. -!!! error TS2345: Property 'raw' is missing in type '{ [x: number]: undefined; }'. +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'TemplateStringsArray'. +!!! error TS2345: Property 'raw' is missing in type '{}'. f `abcdef${ 1234 }${ 5678 }ghijkl`; \ No newline at end of file diff --git a/tests/baselines/reference/typeParameterFixingWithConstraints.types b/tests/baselines/reference/typeParameterFixingWithConstraints.types index 22d294f9d2e..1e71f439e5e 100644 --- a/tests/baselines/reference/typeParameterFixingWithConstraints.types +++ b/tests/baselines/reference/typeParameterFixingWithConstraints.types @@ -31,17 +31,17 @@ var foo: IFoo; >IFoo : IFoo foo.foo({ bar: null }, bar => null, bar => null); ->foo.foo({ bar: null }, bar => null, bar => null) : { [x: string]: any; bar: any; } +>foo.foo({ bar: null }, bar => null, bar => null) : { bar: any; } >foo.foo : (bar: TBar, bar1: (bar: TBar) => TBar, bar2: (bar: TBar) => TBar) => TBar >foo : IFoo >foo : (bar: TBar, bar1: (bar: TBar) => TBar, bar2: (bar: TBar) => TBar) => TBar ->{ bar: null } : { [x: string]: null; bar: null; } +>{ bar: null } : { bar: null; } >bar : null >null : null ->bar => null : (bar: { [x: string]: any; bar: any; }) => any ->bar : { [x: string]: any; bar: any; } +>bar => null : (bar: { bar: any; }) => any +>bar : { bar: any; } >null : null ->bar => null : (bar: { [x: string]: any; bar: any; }) => any ->bar : { [x: string]: any; bar: any; } +>bar => null : (bar: { bar: any; }) => any +>bar : { bar: any; } >null : null diff --git a/tests/baselines/reference/underscoreTest1.types b/tests/baselines/reference/underscoreTest1.types index 4d8a05a5c38..37f3bdb12ab 100644 --- a/tests/baselines/reference/underscoreTest1.types +++ b/tests/baselines/reference/underscoreTest1.types @@ -31,7 +31,7 @@ _.each({ one: 1, two: 2, three: 3 }, (value: number, key?: string) => alert(valu >_.each : { (list: T[], iterator: Iterator, context?: any): void; (list: Dictionary, iterator: Iterator, context?: any): void; } >_ : Underscore.Static >each : { (list: T[], iterator: Iterator, context?: any): void; (list: Dictionary, iterator: Iterator, context?: any): void; } ->{ one: 1, two: 2, three: 3 } : { [x: string]: number; one: number; two: number; three: number; } +>{ one: 1, two: 2, three: 3 } : { one: number; two: number; three: number; } >one : number >1 : number >two : number @@ -68,7 +68,7 @@ _.map({ one: 1, two: 2, three: 3 }, (value: number, key?: string) => value * 3); >_.map : { (list: T[], iterator: Iterator, context?: any): U[]; (list: Dictionary, iterator: Iterator, context?: any): U[]; } >_ : Underscore.Static >map : { (list: T[], iterator: Iterator, context?: any): U[]; (list: Dictionary, iterator: Iterator, context?: any): U[]; } ->{ one: 1, two: 2, three: 3 } : { [x: string]: number; one: number; two: number; three: number; } +>{ one: 1, two: 2, three: 3 } : { one: number; two: number; three: number; } >one : number >1 : number >two : number @@ -449,7 +449,7 @@ _.size({ one: 1, two: 2, three: 3 }); >_.size : { (list: T[]): number; (list: Dictionary): number; } >_ : Underscore.Static >size : { (list: T[]): number; (list: Dictionary): number; } ->{ one: 1, two: 2, three: 3 } : { [x: string]: number; one: number; two: number; three: number; } +>{ one: 1, two: 2, three: 3 } : { one: number; two: number; three: number; } >one : number >1 : number >two : number diff --git a/tests/baselines/reference/widenedTypes.errors.txt b/tests/baselines/reference/widenedTypes.errors.txt index 2277ef6eea7..beda634c2a6 100644 --- a/tests/baselines/reference/widenedTypes.errors.txt +++ b/tests/baselines/reference/widenedTypes.errors.txt @@ -6,8 +6,8 @@ tests/cases/compiler/widenedTypes.ts(11,1): error TS2322: Type 'string' is not a tests/cases/compiler/widenedTypes.ts(18,1): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/compiler/widenedTypes.ts(23,5): error TS2322: Type 'number[]' is not assignable to type 'string[]'. Type 'number' is not assignable to type 'string'. -tests/cases/compiler/widenedTypes.ts(24,5): error TS2322: Type '{ [x: string]: number; x: number; y: null; }' is not assignable to type '{ [x: string]: string; }'. - Index signatures are incompatible. +tests/cases/compiler/widenedTypes.ts(24,5): error TS2322: Type '{ x: number; y: null; }' is not assignable to type '{ [x: string]: string; }'. + Property 'x' is incompatible with index signature. Type 'number' is not assignable to type 'string'. @@ -52,6 +52,6 @@ tests/cases/compiler/widenedTypes.ts(24,5): error TS2322: Type '{ [x: string]: n !!! error TS2322: Type 'number' is not assignable to type 'string'. var obj: { [x: string]: string; } = { x: 3, y: null }; // assignable because null is widened, and therefore BCT is any ~~~ -!!! error TS2322: Type '{ [x: string]: number; x: number; y: null; }' is not assignable to type '{ [x: string]: string; }'. -!!! error TS2322: Index signatures are incompatible. +!!! error TS2322: Type '{ x: number; y: null; }' is not assignable to type '{ [x: string]: string; }'. +!!! error TS2322: Property 'x' is incompatible with index signature. !!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt b/tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt index 60602351b46..47e8e6ff4f2 100644 --- a/tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt +++ b/tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts(13,12): error TS2345: Argument of type '{ [x: number]: undefined; length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'. +tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts(13,12): error TS2345: Argument of type '{ length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'. ==== tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts (1 errors) ==== @@ -16,4 +16,4 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursi var r = c.foo(''); var r2 = r({ length: 3, charAt: (x: number) => { '' } }); // error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ [x: number]: undefined; length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'. \ No newline at end of file +!!! error TS2345: Argument of type '{ length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'. \ No newline at end of file From bb7ba1dcea57c6ac35391a271e7f5aec0d636972 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 10 Feb 2016 06:56:25 -0800 Subject: [PATCH 15/55] Adding test --- .../reference/implicitIndexSignatures.js | 85 ++++++++ .../reference/implicitIndexSignatures.symbols | 166 +++++++++++++++ .../reference/implicitIndexSignatures.types | 201 ++++++++++++++++++ .../cases/compiler/implicitIndexSignatures.ts | 45 ++++ 4 files changed, 497 insertions(+) create mode 100644 tests/baselines/reference/implicitIndexSignatures.js create mode 100644 tests/baselines/reference/implicitIndexSignatures.symbols create mode 100644 tests/baselines/reference/implicitIndexSignatures.types create mode 100644 tests/cases/compiler/implicitIndexSignatures.ts diff --git a/tests/baselines/reference/implicitIndexSignatures.js b/tests/baselines/reference/implicitIndexSignatures.js new file mode 100644 index 00000000000..6403f1f1543 --- /dev/null +++ b/tests/baselines/reference/implicitIndexSignatures.js @@ -0,0 +1,85 @@ +//// [implicitIndexSignatures.ts] +type StringMap = { [x: string]: string }; + +const empty1 = {}; +let empty2: {}; +const names1 = { a: "foo", b: "bar" }; +let names2: { a: string, b: string }; +let map: StringMap; +map = { x: "xxx", y: "yyy" }; +map = empty1; +map = empty2; +map = names1; +map = names2; + +declare function getStringIndexValue(map: { [x: string]: T }): T; +declare function getNumberIndexValue(map: { [x: number]: T }): T; + +function f1() { + const o1 = { a: 1, b: 2 }; + let o2: { a: number, b: number }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f2() { + const o1 = { a: "1", b: "2" }; + let o2: { a: string, b: string }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f3() { + const o1 = { a: 1, b: "2" }; + let o2: { a: number, b: string }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f4() { + const o1 = { 0: "0", 1: "1", count: 2 }; + let o2: { 0: string, 1: string, count: number }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); + const v3 = getNumberIndexValue(o1); + const v4 = getNumberIndexValue(o2); +} + + +//// [implicitIndexSignatures.js] +var empty1 = {}; +var empty2; +var names1 = { a: "foo", b: "bar" }; +var names2; +var map; +map = { x: "xxx", y: "yyy" }; +map = empty1; +map = empty2; +map = names1; +map = names2; +function f1() { + var o1 = { a: 1, b: 2 }; + var o2; + var v1 = getStringIndexValue(o1); + var v2 = getStringIndexValue(o2); +} +function f2() { + var o1 = { a: "1", b: "2" }; + var o2; + var v1 = getStringIndexValue(o1); + var v2 = getStringIndexValue(o2); +} +function f3() { + var o1 = { a: 1, b: "2" }; + var o2; + var v1 = getStringIndexValue(o1); + var v2 = getStringIndexValue(o2); +} +function f4() { + var o1 = { 0: "0", 1: "1", count: 2 }; + var o2; + var v1 = getStringIndexValue(o1); + var v2 = getStringIndexValue(o2); + var v3 = getNumberIndexValue(o1); + var v4 = getNumberIndexValue(o2); +} diff --git a/tests/baselines/reference/implicitIndexSignatures.symbols b/tests/baselines/reference/implicitIndexSignatures.symbols new file mode 100644 index 00000000000..a014e7e942c --- /dev/null +++ b/tests/baselines/reference/implicitIndexSignatures.symbols @@ -0,0 +1,166 @@ +=== tests/cases/compiler/implicitIndexSignatures.ts === +type StringMap = { [x: string]: string }; +>StringMap : Symbol(StringMap, Decl(implicitIndexSignatures.ts, 0, 0)) +>x : Symbol(x, Decl(implicitIndexSignatures.ts, 0, 20)) + +const empty1 = {}; +>empty1 : Symbol(empty1, Decl(implicitIndexSignatures.ts, 2, 5)) + +let empty2: {}; +>empty2 : Symbol(empty2, Decl(implicitIndexSignatures.ts, 3, 3)) + +const names1 = { a: "foo", b: "bar" }; +>names1 : Symbol(names1, Decl(implicitIndexSignatures.ts, 4, 5)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 4, 16)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 4, 26)) + +let names2: { a: string, b: string }; +>names2 : Symbol(names2, Decl(implicitIndexSignatures.ts, 5, 3)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 5, 13)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 5, 24)) + +let map: StringMap; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>StringMap : Symbol(StringMap, Decl(implicitIndexSignatures.ts, 0, 0)) + +map = { x: "xxx", y: "yyy" }; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>x : Symbol(x, Decl(implicitIndexSignatures.ts, 7, 7)) +>y : Symbol(y, Decl(implicitIndexSignatures.ts, 7, 17)) + +map = empty1; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>empty1 : Symbol(empty1, Decl(implicitIndexSignatures.ts, 2, 5)) + +map = empty2; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>empty2 : Symbol(empty2, Decl(implicitIndexSignatures.ts, 3, 3)) + +map = names1; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>names1 : Symbol(names1, Decl(implicitIndexSignatures.ts, 4, 5)) + +map = names2; +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 6, 3)) +>names2 : Symbol(names2, Decl(implicitIndexSignatures.ts, 5, 3)) + +declare function getStringIndexValue(map: { [x: string]: T }): T; +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 13, 37)) +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 13, 40)) +>x : Symbol(x, Decl(implicitIndexSignatures.ts, 13, 48)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 13, 37)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 13, 37)) + +declare function getNumberIndexValue(map: { [x: number]: T }): T; +>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 14, 37)) +>map : Symbol(map, Decl(implicitIndexSignatures.ts, 14, 40)) +>x : Symbol(x, Decl(implicitIndexSignatures.ts, 14, 48)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 14, 37)) +>T : Symbol(T, Decl(implicitIndexSignatures.ts, 14, 37)) + +function f1() { +>f1 : Symbol(f1, Decl(implicitIndexSignatures.ts, 14, 68)) + + const o1 = { a: 1, b: 2 }; +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 17, 9)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 17, 16)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 17, 22)) + + let o2: { a: number, b: number }; +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 18, 7)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 18, 13)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 18, 24)) + + const v1 = getStringIndexValue(o1); +>v1 : Symbol(v1, Decl(implicitIndexSignatures.ts, 19, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 17, 9)) + + const v2 = getStringIndexValue(o2); +>v2 : Symbol(v2, Decl(implicitIndexSignatures.ts, 20, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 18, 7)) +} + +function f2() { +>f2 : Symbol(f2, Decl(implicitIndexSignatures.ts, 21, 1)) + + const o1 = { a: "1", b: "2" }; +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 24, 9)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 24, 16)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 24, 24)) + + let o2: { a: string, b: string }; +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 25, 7)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 25, 13)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 25, 24)) + + const v1 = getStringIndexValue(o1); +>v1 : Symbol(v1, Decl(implicitIndexSignatures.ts, 26, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 24, 9)) + + const v2 = getStringIndexValue(o2); +>v2 : Symbol(v2, Decl(implicitIndexSignatures.ts, 27, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 25, 7)) +} + +function f3() { +>f3 : Symbol(f3, Decl(implicitIndexSignatures.ts, 28, 1)) + + const o1 = { a: 1, b: "2" }; +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 31, 9)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 31, 16)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 31, 22)) + + let o2: { a: number, b: string }; +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 32, 7)) +>a : Symbol(a, Decl(implicitIndexSignatures.ts, 32, 13)) +>b : Symbol(b, Decl(implicitIndexSignatures.ts, 32, 24)) + + const v1 = getStringIndexValue(o1); +>v1 : Symbol(v1, Decl(implicitIndexSignatures.ts, 33, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 31, 9)) + + const v2 = getStringIndexValue(o2); +>v2 : Symbol(v2, Decl(implicitIndexSignatures.ts, 34, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 32, 7)) +} + +function f4() { +>f4 : Symbol(f4, Decl(implicitIndexSignatures.ts, 35, 1)) + + const o1 = { 0: "0", 1: "1", count: 2 }; +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 38, 9)) +>count : Symbol(count, Decl(implicitIndexSignatures.ts, 38, 32)) + + let o2: { 0: string, 1: string, count: number }; +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 39, 7)) +>count : Symbol(count, Decl(implicitIndexSignatures.ts, 39, 35)) + + const v1 = getStringIndexValue(o1); +>v1 : Symbol(v1, Decl(implicitIndexSignatures.ts, 40, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 38, 9)) + + const v2 = getStringIndexValue(o2); +>v2 : Symbol(v2, Decl(implicitIndexSignatures.ts, 41, 9)) +>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13)) +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 39, 7)) + + const v3 = getNumberIndexValue(o1); +>v3 : Symbol(v3, Decl(implicitIndexSignatures.ts, 42, 9)) +>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68)) +>o1 : Symbol(o1, Decl(implicitIndexSignatures.ts, 38, 9)) + + const v4 = getNumberIndexValue(o2); +>v4 : Symbol(v4, Decl(implicitIndexSignatures.ts, 43, 9)) +>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68)) +>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 39, 7)) +} + diff --git a/tests/baselines/reference/implicitIndexSignatures.types b/tests/baselines/reference/implicitIndexSignatures.types new file mode 100644 index 00000000000..7d335c00491 --- /dev/null +++ b/tests/baselines/reference/implicitIndexSignatures.types @@ -0,0 +1,201 @@ +=== tests/cases/compiler/implicitIndexSignatures.ts === +type StringMap = { [x: string]: string }; +>StringMap : { [x: string]: string; } +>x : string + +const empty1 = {}; +>empty1 : {} +>{} : {} + +let empty2: {}; +>empty2 : {} + +const names1 = { a: "foo", b: "bar" }; +>names1 : { a: string; b: string; } +>{ a: "foo", b: "bar" } : { a: string; b: string; } +>a : string +>"foo" : string +>b : string +>"bar" : string + +let names2: { a: string, b: string }; +>names2 : { a: string; b: string; } +>a : string +>b : string + +let map: StringMap; +>map : { [x: string]: string; } +>StringMap : { [x: string]: string; } + +map = { x: "xxx", y: "yyy" }; +>map = { x: "xxx", y: "yyy" } : { x: string; y: string; } +>map : { [x: string]: string; } +>{ x: "xxx", y: "yyy" } : { x: string; y: string; } +>x : string +>"xxx" : string +>y : string +>"yyy" : string + +map = empty1; +>map = empty1 : {} +>map : { [x: string]: string; } +>empty1 : {} + +map = empty2; +>map = empty2 : {} +>map : { [x: string]: string; } +>empty2 : {} + +map = names1; +>map = names1 : { a: string; b: string; } +>map : { [x: string]: string; } +>names1 : { a: string; b: string; } + +map = names2; +>map = names2 : { a: string; b: string; } +>map : { [x: string]: string; } +>names2 : { a: string; b: string; } + +declare function getStringIndexValue(map: { [x: string]: T }): T; +>getStringIndexValue : (map: { [x: string]: T; }) => T +>T : T +>map : { [x: string]: T; } +>x : string +>T : T +>T : T + +declare function getNumberIndexValue(map: { [x: number]: T }): T; +>getNumberIndexValue : (map: { [x: number]: T; }) => T +>T : T +>map : { [x: number]: T; } +>x : number +>T : T +>T : T + +function f1() { +>f1 : () => void + + const o1 = { a: 1, b: 2 }; +>o1 : { a: number; b: number; } +>{ a: 1, b: 2 } : { a: number; b: number; } +>a : number +>1 : number +>b : number +>2 : number + + let o2: { a: number, b: number }; +>o2 : { a: number; b: number; } +>a : number +>b : number + + const v1 = getStringIndexValue(o1); +>v1 : number +>getStringIndexValue(o1) : number +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o1 : { a: number; b: number; } + + const v2 = getStringIndexValue(o2); +>v2 : number +>getStringIndexValue(o2) : number +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o2 : { a: number; b: number; } +} + +function f2() { +>f2 : () => void + + const o1 = { a: "1", b: "2" }; +>o1 : { a: string; b: string; } +>{ a: "1", b: "2" } : { a: string; b: string; } +>a : string +>"1" : string +>b : string +>"2" : string + + let o2: { a: string, b: string }; +>o2 : { a: string; b: string; } +>a : string +>b : string + + const v1 = getStringIndexValue(o1); +>v1 : string +>getStringIndexValue(o1) : string +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o1 : { a: string; b: string; } + + const v2 = getStringIndexValue(o2); +>v2 : string +>getStringIndexValue(o2) : string +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o2 : { a: string; b: string; } +} + +function f3() { +>f3 : () => void + + const o1 = { a: 1, b: "2" }; +>o1 : { a: number; b: string; } +>{ a: 1, b: "2" } : { a: number; b: string; } +>a : number +>1 : number +>b : string +>"2" : string + + let o2: { a: number, b: string }; +>o2 : { a: number; b: string; } +>a : number +>b : string + + const v1 = getStringIndexValue(o1); +>v1 : number | string +>getStringIndexValue(o1) : number | string +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o1 : { a: number; b: string; } + + const v2 = getStringIndexValue(o2); +>v2 : number | string +>getStringIndexValue(o2) : number | string +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o2 : { a: number; b: string; } +} + +function f4() { +>f4 : () => void + + const o1 = { 0: "0", 1: "1", count: 2 }; +>o1 : { 0: string; 1: string; count: number; } +>{ 0: "0", 1: "1", count: 2 } : { 0: string; 1: string; count: number; } +>"0" : string +>"1" : string +>count : number +>2 : number + + let o2: { 0: string, 1: string, count: number }; +>o2 : { 0: string; 1: string; count: number; } +>count : number + + const v1 = getStringIndexValue(o1); +>v1 : string | number +>getStringIndexValue(o1) : string | number +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o1 : { 0: string; 1: string; count: number; } + + const v2 = getStringIndexValue(o2); +>v2 : string | number +>getStringIndexValue(o2) : string | number +>getStringIndexValue : (map: { [x: string]: T; }) => T +>o2 : { 0: string; 1: string; count: number; } + + const v3 = getNumberIndexValue(o1); +>v3 : string +>getNumberIndexValue(o1) : string +>getNumberIndexValue : (map: { [x: number]: T; }) => T +>o1 : { 0: string; 1: string; count: number; } + + const v4 = getNumberIndexValue(o2); +>v4 : string +>getNumberIndexValue(o2) : string +>getNumberIndexValue : (map: { [x: number]: T; }) => T +>o2 : { 0: string; 1: string; count: number; } +} + diff --git a/tests/cases/compiler/implicitIndexSignatures.ts b/tests/cases/compiler/implicitIndexSignatures.ts new file mode 100644 index 00000000000..2e36a91bada --- /dev/null +++ b/tests/cases/compiler/implicitIndexSignatures.ts @@ -0,0 +1,45 @@ +type StringMap = { [x: string]: string }; + +const empty1 = {}; +let empty2: {}; +const names1 = { a: "foo", b: "bar" }; +let names2: { a: string, b: string }; +let map: StringMap; +map = { x: "xxx", y: "yyy" }; +map = empty1; +map = empty2; +map = names1; +map = names2; + +declare function getStringIndexValue(map: { [x: string]: T }): T; +declare function getNumberIndexValue(map: { [x: number]: T }): T; + +function f1() { + const o1 = { a: 1, b: 2 }; + let o2: { a: number, b: number }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f2() { + const o1 = { a: "1", b: "2" }; + let o2: { a: string, b: string }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f3() { + const o1 = { a: 1, b: "2" }; + let o2: { a: number, b: string }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); +} + +function f4() { + const o1 = { 0: "0", 1: "1", count: 2 }; + let o2: { 0: string, 1: string, count: number }; + const v1 = getStringIndexValue(o1); + const v2 = getStringIndexValue(o2); + const v3 = getNumberIndexValue(o1); + const v4 = getNumberIndexValue(o2); +} From 5f95c75403a0fbc36032b1cc7865ff2e73bf5233 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Feb 2016 06:18:04 -0800 Subject: [PATCH 16/55] Simplify indexTypesRelatedTo function --- src/compiler/checker.ts | 112 ++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 73 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e6dec1d45ad..0dd945bcb98 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5760,9 +5760,9 @@ namespace ts { if (result) { result &= signaturesRelatedTo(source, target, SignatureKind.Construct, reportErrors); if (result) { - result &= stringIndexTypesRelatedTo(source, originalSource, target, reportErrors); + result &= indexTypesRelatedTo(source, originalSource, target, IndexKind.String, reportErrors); if (result) { - result &= numberIndexTypesRelatedTo(source, originalSource, target, reportErrors); + result &= indexTypesRelatedTo(source, originalSource, target, IndexKind.Number, reportErrors); } } } @@ -5964,10 +5964,10 @@ namespace ts { return result; } - function eachPropertyRelatedTo(source: Type, target: Type, numericPropertiesOnly: boolean, reportErrors: boolean): Ternary { + function eachPropertyRelatedTo(source: Type, target: Type, kind: IndexKind, reportErrors: boolean): Ternary { let result = Ternary.True; for (const prop of getPropertiesOfObjectType(source)) { - if (!numericPropertiesOnly || isNumericLiteralName(prop.name)) { + if (kind === IndexKind.String || isNumericLiteralName(prop.name)) { const related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); if (!related) { if (reportErrors) { @@ -5981,82 +5981,48 @@ namespace ts { return result; } - function stringIndexTypesRelatedTo(source: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { - if (relation === identityRelation) { - return indexTypesIdenticalTo(IndexKind.String, source, target); + function indexInfoRelatedTo(sourceInfo: IndexInfo, targetInfo: IndexInfo, reportErrors: boolean) { + const related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); + if (!related && reportErrors) { + reportError(Diagnostics.Index_signatures_are_incompatible); } - const targetInfo = getIndexInfoOfType(target, IndexKind.String); - if (targetInfo) { - if ((targetInfo.type.flags & TypeFlags.Any) && !(originalSource.flags & TypeFlags.Primitive)) { - // non-primitive assignment to any is always allowed, eg - // `var x: { [index: string]: any } = { property: 12 };` - return Ternary.True; + return related; + } + + function indexTypesRelatedTo(source: Type, originalSource: Type, target: Type, kind: IndexKind, reportErrors: boolean) { + if (relation === identityRelation) { + return indexTypesIdenticalTo(source, target, kind); + } + const targetInfo = getIndexInfoOfType(target, kind); + if (!targetInfo || ((targetInfo.type.flags & TypeFlags.Any) && !(originalSource.flags & TypeFlags.Primitive))) { + // Index signature of type any permits assignment from everything but primitives + return Ternary.True; + } + const sourceInfo = getIndexInfoOfType(source, kind) || + kind === IndexKind.Number && getIndexInfoOfType(source, IndexKind.String); + if (sourceInfo) { + return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors); + } + if (isObjectLiteralType(source)) { + let related = Ternary.True; + if (kind === IndexKind.String) { + const sourceNumberInfo = getIndexInfoOfType(source, IndexKind.Number); + if (sourceNumberInfo) { + related = indexInfoRelatedTo(sourceNumberInfo, targetInfo, reportErrors); + } } - const sourceInfo = getIndexInfoOfType(source, IndexKind.String); - if (!sourceInfo) { - if (isObjectLiteralType(source)) { - return eachPropertyRelatedTo(source, targetInfo.type, /* numericPropertiesOnly*/ false, reportErrors); - } - if (reportErrors) { - reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); - } - return Ternary.False; - } - const related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); - if (!related) { - if (reportErrors) { - reportError(Diagnostics.Index_signatures_are_incompatible); - } - return Ternary.False; + if (related) { + related &= eachPropertyRelatedTo(source, targetInfo.type, kind, reportErrors); } return related; } - return Ternary.True; + if (reportErrors) { + reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); + } + return Ternary.False; } - function numberIndexTypesRelatedTo(source: Type, originalSource: Type, target: Type, reportErrors: boolean): Ternary { - if (relation === identityRelation) { - return indexTypesIdenticalTo(IndexKind.Number, source, target); - } - const targetInfo = getIndexInfoOfType(target, IndexKind.Number); - if (targetInfo) { - if ((targetInfo.type.flags & TypeFlags.Any) && !(originalSource.flags & TypeFlags.Primitive)) { - // non-primitive assignment to any is always allowed, eg - // `var x: { [index: number]: any } = { property: 12 };` - return Ternary.True; - } - const sourceStringInfo = getIndexInfoOfType(source, IndexKind.String); - const sourceNumberInfo = getIndexInfoOfType(source, IndexKind.Number); - if (!(sourceStringInfo || sourceNumberInfo)) { - if (isObjectLiteralType(source)) { - return eachPropertyRelatedTo(source, targetInfo.type, /* numericPropertiesOnly*/ true, reportErrors); - } - if (reportErrors) { - reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); - } - return Ternary.False; - } - let related: Ternary; - if (sourceStringInfo && sourceNumberInfo) { - // If we know for sure we're testing both string and numeric index types then only report errors from the second one - related = isRelatedTo(sourceStringInfo.type, targetInfo.type, /*reportErrors*/ false) || - isRelatedTo(sourceNumberInfo.type, targetInfo.type, reportErrors); - } - else { - related = isRelatedTo((sourceStringInfo || sourceNumberInfo).type, targetInfo.type, reportErrors); - } - if (!related) { - if (reportErrors) { - reportError(Diagnostics.Index_signatures_are_incompatible); - } - return Ternary.False; - } - return related; - } - return Ternary.True; - } - - function indexTypesIdenticalTo(indexKind: IndexKind, source: Type, target: Type): Ternary { + function indexTypesIdenticalTo(source: Type, target: Type, indexKind: IndexKind): Ternary { const targetInfo = getIndexInfoOfType(target, indexKind); const sourceInfo = getIndexInfoOfType(source, indexKind); if (!sourceInfo && !targetInfo) { From b724a094ae49bf35b253262a2f3e01f61733c3dc Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Feb 2016 09:18:26 -0800 Subject: [PATCH 17/55] Infer index signatures when object literals contain computed properties --- src/compiler/checker.ts | 50 ++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0dd945bcb98..611ffb79062 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8243,6 +8243,17 @@ namespace ts { return links.resolvedType; } + function getObjectLiteralIndexInfo(node: ObjectLiteralExpression, properties: Symbol[], kind: IndexKind): IndexInfo { + const propTypes: Type[] = []; + for (let i = 0; i < properties.length; i++) { + if (kind === IndexKind.String || isNumericName(node.properties[i].name)) { + propTypes.push(getTypeOfSymbol(properties[i])); + } + } + const unionType = propTypes.length ? getUnionType(propTypes) : undefinedType; + return createIndexInfo(unionType, /*isReadonly*/ false); + } + function checkObjectLiteral(node: ObjectLiteralExpression, contextualMapper?: TypeMapper): Type { const inDestructuringPattern = isAssignmentTarget(node); // Grammar checking @@ -8254,9 +8265,10 @@ namespace ts { const contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression); let typeFlags: TypeFlags = 0; - let hasDynamicProperties = false; - let patternWithComputedProperties = false; + let hasComputedStringProperty = false; + let hasComputedNumberProperty = false; + for (const memberDecl of node.properties) { let member = memberDecl.symbol; if (memberDecl.kind === SyntaxKind.PropertyAssignment || @@ -8321,7 +8333,12 @@ namespace ts { } if (hasDynamicName(memberDecl)) { - hasDynamicProperties = true; + if (isNumericName(memberDecl.name)) { + hasComputedNumberProperty = true; + } + else { + hasComputedStringProperty = true; + } } else { propertiesTable[member.name] = member; @@ -8344,8 +8361,8 @@ namespace ts { } } - const stringIndexInfo = getIndexInfo(IndexKind.String); - const numberIndexInfo = getIndexInfo(IndexKind.Number); + const stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, propertiesArray, IndexKind.String) : undefined; + const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, IndexKind.Number) : undefined; const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : TypeFlags.FreshObjectLiteral; result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags) | (patternWithComputedProperties ? TypeFlags.ObjectLiteralPatternWithComputedProperties : 0); @@ -8353,29 +8370,6 @@ namespace ts { result.pattern = node; } return result; - - function getIndexInfo(kind: IndexKind) { - if (hasDynamicProperties && contextualType && contextualTypeHasIndexSignature(contextualType, kind)) { - const propTypes: Type[] = []; - for (let i = 0; i < propertiesArray.length; i++) { - const propertyDecl = node.properties[i]; - if (kind === IndexKind.String || isNumericName(propertyDecl.name)) { - // Do not call getSymbolOfNode(propertyDecl), as that will get the - // original symbol for the node. We actually want to get the symbol - // created by checkObjectLiteral, since that will be appropriately - // contextually typed and resolved. - const type = getTypeOfSymbol(propertiesArray[i]); - if (!contains(propTypes, type)) { - propTypes.push(type); - } - } - } - const unionType = propTypes.length ? getUnionType(propTypes) : undefinedType; - typeFlags |= unionType.flags; - return createIndexInfo(unionType, /*isReadonly*/ false); - } - return undefined; - } } function checkJsxSelfClosingElement(node: JsxSelfClosingElement) { From 678d59159586b92849c948e0056792d56933a70d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Feb 2016 09:24:23 -0800 Subject: [PATCH 18/55] Accepting new baselines --- tests/baselines/reference/computedPropertyNames10_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames10_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames11_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames11_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames18_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames18_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames1_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames1_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames20_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames20_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames22_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames22_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames25_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames25_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames28_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames28_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames29_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames29_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames31_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames31_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames33_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames33_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames46_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames46_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames47_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames47_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames48_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames48_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames4_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames4_ES6.types | 4 ++-- tests/baselines/reference/computedPropertyNames7_ES5.types | 4 ++-- tests/baselines/reference/computedPropertyNames7_ES6.types | 4 ++-- .../reference/computedPropertyNamesContextualType1_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType1_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType2_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType2_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType3_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType3_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType4_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType4_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType5_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType5_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType6_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType6_ES6.types | 2 +- .../reference/computedPropertyNamesContextualType7_ES5.types | 2 +- .../reference/computedPropertyNamesContextualType7_ES6.types | 2 +- .../computedPropertyNamesContextualType8_ES5.errors.txt | 4 ++-- .../computedPropertyNamesContextualType8_ES6.errors.txt | 4 ++-- .../computedPropertyNamesContextualType9_ES5.errors.txt | 4 ++-- .../computedPropertyNamesContextualType9_ES6.errors.txt | 4 ++-- .../reference/computedPropertyNamesDeclarationEmit5_ES5.js | 4 +++- .../reference/computedPropertyNamesDeclarationEmit5_ES5.types | 4 ++-- .../reference/computedPropertyNamesDeclarationEmit5_ES6.js | 4 +++- .../reference/computedPropertyNamesDeclarationEmit5_ES6.types | 4 ++-- tests/baselines/reference/generatorTypeCheck41.types | 4 ++-- tests/baselines/reference/generatorTypeCheck42.types | 4 ++-- tests/baselines/reference/generatorTypeCheck43.types | 4 ++-- tests/baselines/reference/generatorTypeCheck44.types | 4 ++-- tests/baselines/reference/symbolProperty1.types | 4 ++-- tests/baselines/reference/symbolProperty2.types | 4 ++-- tests/baselines/reference/symbolProperty4.types | 4 ++-- 61 files changed, 110 insertions(+), 106 deletions(-) diff --git a/tests/baselines/reference/computedPropertyNames10_ES5.types b/tests/baselines/reference/computedPropertyNames10_ES5.types index 9dea9cfca93..b9d999397ab 100644 --- a/tests/baselines/reference/computedPropertyNames10_ES5.types +++ b/tests/baselines/reference/computedPropertyNames10_ES5.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { [0](): void; [""](): void; } ->{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [0](): void; [""](): void; } +>v : { [x: string]: () => void; [x: number]: () => void; [0](): void; [""](): void; } +>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [0](): void; [""](): void; } [s]() { }, >s : string diff --git a/tests/baselines/reference/computedPropertyNames10_ES6.types b/tests/baselines/reference/computedPropertyNames10_ES6.types index d2faa138980..1883febb39b 100644 --- a/tests/baselines/reference/computedPropertyNames10_ES6.types +++ b/tests/baselines/reference/computedPropertyNames10_ES6.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { [0](): void; [""](): void; } ->{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [0](): void; [""](): void; } +>v : { [x: string]: () => void; [x: number]: () => void; [0](): void; [""](): void; } +>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [0](): void; [""](): void; } [s]() { }, >s : string diff --git a/tests/baselines/reference/computedPropertyNames11_ES5.types b/tests/baselines/reference/computedPropertyNames11_ES5.types index ae626ec3cba..e15b46cc6c1 100644 --- a/tests/baselines/reference/computedPropertyNames11_ES5.types +++ b/tests/baselines/reference/computedPropertyNames11_ES5.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { readonly [0]: number; [""]: any; } ->{ get [s]() { return 0; }, set [n](v) { }, get [s + s]() { return 0; }, set [s + n](v) { }, get [+s]() { return 0; }, set [""](v) { }, get [0]() { return 0; }, set [a](v) { }, get [true]() { return 0; }, set [`hello bye`](v) { }, get [`hello ${a} bye`]() { return 0; }} : { readonly [0]: number; [""]: any; } +>v : { [x: string]: any; [x: number]: any; readonly [0]: number; [""]: any; } +>{ get [s]() { return 0; }, set [n](v) { }, get [s + s]() { return 0; }, set [s + n](v) { }, get [+s]() { return 0; }, set [""](v) { }, get [0]() { return 0; }, set [a](v) { }, get [true]() { return 0; }, set [`hello bye`](v) { }, get [`hello ${a} bye`]() { return 0; }} : { [x: string]: any; [x: number]: any; readonly [0]: number; [""]: any; } get [s]() { return 0; }, >s : string diff --git a/tests/baselines/reference/computedPropertyNames11_ES6.types b/tests/baselines/reference/computedPropertyNames11_ES6.types index 06dc26e9843..eb5fc105a7a 100644 --- a/tests/baselines/reference/computedPropertyNames11_ES6.types +++ b/tests/baselines/reference/computedPropertyNames11_ES6.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { readonly [0]: number; [""]: any; } ->{ get [s]() { return 0; }, set [n](v) { }, get [s + s]() { return 0; }, set [s + n](v) { }, get [+s]() { return 0; }, set [""](v) { }, get [0]() { return 0; }, set [a](v) { }, get [true]() { return 0; }, set [`hello bye`](v) { }, get [`hello ${a} bye`]() { return 0; }} : { readonly [0]: number; [""]: any; } +>v : { [x: string]: any; [x: number]: any; readonly [0]: number; [""]: any; } +>{ get [s]() { return 0; }, set [n](v) { }, get [s + s]() { return 0; }, set [s + n](v) { }, get [+s]() { return 0; }, set [""](v) { }, get [0]() { return 0; }, set [a](v) { }, get [true]() { return 0; }, set [`hello bye`](v) { }, get [`hello ${a} bye`]() { return 0; }} : { [x: string]: any; [x: number]: any; readonly [0]: number; [""]: any; } get [s]() { return 0; }, >s : string diff --git a/tests/baselines/reference/computedPropertyNames18_ES5.types b/tests/baselines/reference/computedPropertyNames18_ES5.types index c60ab32d3f8..d6808a8edc0 100644 --- a/tests/baselines/reference/computedPropertyNames18_ES5.types +++ b/tests/baselines/reference/computedPropertyNames18_ES5.types @@ -3,8 +3,8 @@ function foo() { >foo : () => void var obj = { ->obj : {} ->{ [this.bar]: 0 } : {} +>obj : { [x: number]: number; } +>{ [this.bar]: 0 } : { [x: number]: number; } [this.bar]: 0 >this.bar : any diff --git a/tests/baselines/reference/computedPropertyNames18_ES6.types b/tests/baselines/reference/computedPropertyNames18_ES6.types index 33a15b6c5a9..11d58e48c69 100644 --- a/tests/baselines/reference/computedPropertyNames18_ES6.types +++ b/tests/baselines/reference/computedPropertyNames18_ES6.types @@ -3,8 +3,8 @@ function foo() { >foo : () => void var obj = { ->obj : {} ->{ [this.bar]: 0 } : {} +>obj : { [x: number]: number; } +>{ [this.bar]: 0 } : { [x: number]: number; } [this.bar]: 0 >this.bar : any diff --git a/tests/baselines/reference/computedPropertyNames1_ES5.types b/tests/baselines/reference/computedPropertyNames1_ES5.types index 6c94c846f30..bcff3b34177 100644 --- a/tests/baselines/reference/computedPropertyNames1_ES5.types +++ b/tests/baselines/reference/computedPropertyNames1_ES5.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames1_ES5.ts === var v = { ->v : {} ->{ get [0 + 1]() { return 0 }, set [0 + 1](v: string) { } //No error} : {} +>v : { [x: number]: number | string; } +>{ get [0 + 1]() { return 0 }, set [0 + 1](v: string) { } //No error} : { [x: number]: number | string; } get [0 + 1]() { return 0 }, >0 + 1 : number diff --git a/tests/baselines/reference/computedPropertyNames1_ES6.types b/tests/baselines/reference/computedPropertyNames1_ES6.types index 95e5a011ea2..df2237a463f 100644 --- a/tests/baselines/reference/computedPropertyNames1_ES6.types +++ b/tests/baselines/reference/computedPropertyNames1_ES6.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames1_ES6.ts === var v = { ->v : {} ->{ get [0 + 1]() { return 0 }, set [0 + 1](v: string) { } //No error} : {} +>v : { [x: number]: number | string; } +>{ get [0 + 1]() { return 0 }, set [0 + 1](v: string) { } //No error} : { [x: number]: number | string; } get [0 + 1]() { return 0 }, >0 + 1 : number diff --git a/tests/baselines/reference/computedPropertyNames20_ES5.types b/tests/baselines/reference/computedPropertyNames20_ES5.types index 91cc2c42963..65ee55be379 100644 --- a/tests/baselines/reference/computedPropertyNames20_ES5.types +++ b/tests/baselines/reference/computedPropertyNames20_ES5.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames20_ES5.ts === var obj = { ->obj : {} ->{ [this.bar]: 0} : {} +>obj : { [x: number]: number; } +>{ [this.bar]: 0} : { [x: number]: number; } [this.bar]: 0 >this.bar : any diff --git a/tests/baselines/reference/computedPropertyNames20_ES6.types b/tests/baselines/reference/computedPropertyNames20_ES6.types index 4ef6f675cce..91e5c8d7843 100644 --- a/tests/baselines/reference/computedPropertyNames20_ES6.types +++ b/tests/baselines/reference/computedPropertyNames20_ES6.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames20_ES6.ts === var obj = { ->obj : {} ->{ [this.bar]: 0} : {} +>obj : { [x: number]: number; } +>{ [this.bar]: 0} : { [x: number]: number; } [this.bar]: 0 >this.bar : any diff --git a/tests/baselines/reference/computedPropertyNames22_ES5.types b/tests/baselines/reference/computedPropertyNames22_ES5.types index ca59250825c..eeec22d2e61 100644 --- a/tests/baselines/reference/computedPropertyNames22_ES5.types +++ b/tests/baselines/reference/computedPropertyNames22_ES5.types @@ -6,8 +6,8 @@ class C { >bar : () => number var obj = { ->obj : {} ->{ [this.bar()]() { } } : {} +>obj : { [x: number]: () => void; } +>{ [this.bar()]() { } } : { [x: number]: () => void; } [this.bar()]() { } >this.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames22_ES6.types b/tests/baselines/reference/computedPropertyNames22_ES6.types index d5fbce29f7d..af9ef9d3a31 100644 --- a/tests/baselines/reference/computedPropertyNames22_ES6.types +++ b/tests/baselines/reference/computedPropertyNames22_ES6.types @@ -6,8 +6,8 @@ class C { >bar : () => number var obj = { ->obj : {} ->{ [this.bar()]() { } } : {} +>obj : { [x: number]: () => void; } +>{ [this.bar()]() { } } : { [x: number]: () => void; } [this.bar()]() { } >this.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames25_ES5.types b/tests/baselines/reference/computedPropertyNames25_ES5.types index 6ca67cce410..f1acc296709 100644 --- a/tests/baselines/reference/computedPropertyNames25_ES5.types +++ b/tests/baselines/reference/computedPropertyNames25_ES5.types @@ -17,8 +17,8 @@ class C extends Base { >foo : () => number var obj = { ->obj : {} ->{ [super.bar()]() { } } : {} +>obj : { [x: number]: () => void; } +>{ [super.bar()]() { } } : { [x: number]: () => void; } [super.bar()]() { } >super.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames25_ES6.types b/tests/baselines/reference/computedPropertyNames25_ES6.types index 1c093ebc59f..26c38013906 100644 --- a/tests/baselines/reference/computedPropertyNames25_ES6.types +++ b/tests/baselines/reference/computedPropertyNames25_ES6.types @@ -17,8 +17,8 @@ class C extends Base { >foo : () => number var obj = { ->obj : {} ->{ [super.bar()]() { } } : {} +>obj : { [x: number]: () => void; } +>{ [super.bar()]() { } } : { [x: number]: () => void; } [super.bar()]() { } >super.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames28_ES5.types b/tests/baselines/reference/computedPropertyNames28_ES5.types index 273dcd426d8..89a2a7ac017 100644 --- a/tests/baselines/reference/computedPropertyNames28_ES5.types +++ b/tests/baselines/reference/computedPropertyNames28_ES5.types @@ -12,8 +12,8 @@ class C extends Base { >super : typeof Base var obj = { ->obj : {} ->{ [(super(), "prop")]() { } } : {} +>obj : { [x: string]: () => void; } +>{ [(super(), "prop")]() { } } : { [x: string]: () => void; } [(super(), "prop")]() { } >(super(), "prop") : string diff --git a/tests/baselines/reference/computedPropertyNames28_ES6.types b/tests/baselines/reference/computedPropertyNames28_ES6.types index a34fb33f6c7..df020c9a5ff 100644 --- a/tests/baselines/reference/computedPropertyNames28_ES6.types +++ b/tests/baselines/reference/computedPropertyNames28_ES6.types @@ -12,8 +12,8 @@ class C extends Base { >super : typeof Base var obj = { ->obj : {} ->{ [(super(), "prop")]() { } } : {} +>obj : { [x: string]: () => void; } +>{ [(super(), "prop")]() { } } : { [x: string]: () => void; } [(super(), "prop")]() { } >(super(), "prop") : string diff --git a/tests/baselines/reference/computedPropertyNames29_ES5.types b/tests/baselines/reference/computedPropertyNames29_ES5.types index f3448f10857..d2f89ef6b18 100644 --- a/tests/baselines/reference/computedPropertyNames29_ES5.types +++ b/tests/baselines/reference/computedPropertyNames29_ES5.types @@ -9,8 +9,8 @@ class C { >() => { var obj = { [this.bar()]() { } // needs capture }; } : () => void var obj = { ->obj : {} ->{ [this.bar()]() { } // needs capture } : {} +>obj : { [x: number]: () => void; } +>{ [this.bar()]() { } // needs capture } : { [x: number]: () => void; } [this.bar()]() { } // needs capture >this.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames29_ES6.types b/tests/baselines/reference/computedPropertyNames29_ES6.types index cd01d37556a..bb324b2b382 100644 --- a/tests/baselines/reference/computedPropertyNames29_ES6.types +++ b/tests/baselines/reference/computedPropertyNames29_ES6.types @@ -9,8 +9,8 @@ class C { >() => { var obj = { [this.bar()]() { } // needs capture }; } : () => void var obj = { ->obj : {} ->{ [this.bar()]() { } // needs capture } : {} +>obj : { [x: number]: () => void; } +>{ [this.bar()]() { } // needs capture } : { [x: number]: () => void; } [this.bar()]() { } // needs capture >this.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames31_ES5.types b/tests/baselines/reference/computedPropertyNames31_ES5.types index 6c0f2572a06..832b2bc067a 100644 --- a/tests/baselines/reference/computedPropertyNames31_ES5.types +++ b/tests/baselines/reference/computedPropertyNames31_ES5.types @@ -20,8 +20,8 @@ class C extends Base { >() => { var obj = { [super.bar()]() { } // needs capture }; } : () => void var obj = { ->obj : {} ->{ [super.bar()]() { } // needs capture } : {} +>obj : { [x: number]: () => void; } +>{ [super.bar()]() { } // needs capture } : { [x: number]: () => void; } [super.bar()]() { } // needs capture >super.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames31_ES6.types b/tests/baselines/reference/computedPropertyNames31_ES6.types index eaddc036812..4f59b1e8c6d 100644 --- a/tests/baselines/reference/computedPropertyNames31_ES6.types +++ b/tests/baselines/reference/computedPropertyNames31_ES6.types @@ -20,8 +20,8 @@ class C extends Base { >() => { var obj = { [super.bar()]() { } // needs capture }; } : () => void var obj = { ->obj : {} ->{ [super.bar()]() { } // needs capture } : {} +>obj : { [x: number]: () => void; } +>{ [super.bar()]() { } // needs capture } : { [x: number]: () => void; } [super.bar()]() { } // needs capture >super.bar() : number diff --git a/tests/baselines/reference/computedPropertyNames33_ES5.types b/tests/baselines/reference/computedPropertyNames33_ES5.types index f44ac3ca769..7031981481f 100644 --- a/tests/baselines/reference/computedPropertyNames33_ES5.types +++ b/tests/baselines/reference/computedPropertyNames33_ES5.types @@ -12,8 +12,8 @@ class C { >bar : () => number var obj = { ->obj : {} ->{ [foo()]() { } } : {} +>obj : { [x: string]: () => void; } +>{ [foo()]() { } } : { [x: string]: () => void; } [foo()]() { } >foo() : string diff --git a/tests/baselines/reference/computedPropertyNames33_ES6.types b/tests/baselines/reference/computedPropertyNames33_ES6.types index 3081337c8bb..3c57daf2ace 100644 --- a/tests/baselines/reference/computedPropertyNames33_ES6.types +++ b/tests/baselines/reference/computedPropertyNames33_ES6.types @@ -12,8 +12,8 @@ class C { >bar : () => number var obj = { ->obj : {} ->{ [foo()]() { } } : {} +>obj : { [x: string]: () => void; } +>{ [foo()]() { } } : { [x: string]: () => void; } [foo()]() { } >foo() : string diff --git a/tests/baselines/reference/computedPropertyNames46_ES5.types b/tests/baselines/reference/computedPropertyNames46_ES5.types index 394b22bd904..b8df0d56e13 100644 --- a/tests/baselines/reference/computedPropertyNames46_ES5.types +++ b/tests/baselines/reference/computedPropertyNames46_ES5.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES5.ts === var o = { ->o : {} ->{ ["" || 0]: 0} : {} +>o : { [x: string]: number; } +>{ ["" || 0]: 0} : { [x: string]: number; } ["" || 0]: 0 >"" || 0 : string | number diff --git a/tests/baselines/reference/computedPropertyNames46_ES6.types b/tests/baselines/reference/computedPropertyNames46_ES6.types index 864fd81321d..a786eca200c 100644 --- a/tests/baselines/reference/computedPropertyNames46_ES6.types +++ b/tests/baselines/reference/computedPropertyNames46_ES6.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES6.ts === var o = { ->o : {} ->{ ["" || 0]: 0} : {} +>o : { [x: string]: number; } +>{ ["" || 0]: 0} : { [x: string]: number; } ["" || 0]: 0 >"" || 0 : string | number diff --git a/tests/baselines/reference/computedPropertyNames47_ES5.types b/tests/baselines/reference/computedPropertyNames47_ES5.types index 6aa841ffd62..cdc2c3301f6 100644 --- a/tests/baselines/reference/computedPropertyNames47_ES5.types +++ b/tests/baselines/reference/computedPropertyNames47_ES5.types @@ -8,8 +8,8 @@ enum E2 { x } >x : E2 var o = { ->o : {} ->{ [E1.x || E2.x]: 0} : {} +>o : { [x: number]: number; } +>{ [E1.x || E2.x]: 0} : { [x: number]: number; } [E1.x || E2.x]: 0 >E1.x || E2.x : E1 | E2 diff --git a/tests/baselines/reference/computedPropertyNames47_ES6.types b/tests/baselines/reference/computedPropertyNames47_ES6.types index f038b172ca1..a923e934d66 100644 --- a/tests/baselines/reference/computedPropertyNames47_ES6.types +++ b/tests/baselines/reference/computedPropertyNames47_ES6.types @@ -8,8 +8,8 @@ enum E2 { x } >x : E2 var o = { ->o : {} ->{ [E1.x || E2.x]: 0} : {} +>o : { [x: number]: number; } +>{ [E1.x || E2.x]: 0} : { [x: number]: number; } [E1.x || E2.x]: 0 >E1.x || E2.x : E1 | E2 diff --git a/tests/baselines/reference/computedPropertyNames48_ES5.types b/tests/baselines/reference/computedPropertyNames48_ES5.types index 2b9131a11c8..12752d8abeb 100644 --- a/tests/baselines/reference/computedPropertyNames48_ES5.types +++ b/tests/baselines/reference/computedPropertyNames48_ES5.types @@ -39,9 +39,9 @@ extractIndexer({ }); // Should return string extractIndexer({ ->extractIndexer({ ["" || 0]: ""}) : any +>extractIndexer({ ["" || 0]: ""}) : string >extractIndexer : (p: { [n: number]: T; }) => T ->{ ["" || 0]: ""} : { [x: number]: undefined; } +>{ ["" || 0]: ""} : { [x: string]: string; } ["" || 0]: "" >"" || 0 : string | number diff --git a/tests/baselines/reference/computedPropertyNames48_ES6.types b/tests/baselines/reference/computedPropertyNames48_ES6.types index 2b803b19bd6..832bc3c7025 100644 --- a/tests/baselines/reference/computedPropertyNames48_ES6.types +++ b/tests/baselines/reference/computedPropertyNames48_ES6.types @@ -39,9 +39,9 @@ extractIndexer({ }); // Should return string extractIndexer({ ->extractIndexer({ ["" || 0]: ""}) : any +>extractIndexer({ ["" || 0]: ""}) : string >extractIndexer : (p: { [n: number]: T; }) => T ->{ ["" || 0]: ""} : { [x: number]: undefined; } +>{ ["" || 0]: ""} : { [x: string]: string; } ["" || 0]: "" >"" || 0 : string | number diff --git a/tests/baselines/reference/computedPropertyNames4_ES5.types b/tests/baselines/reference/computedPropertyNames4_ES5.types index 6984d2e69b8..3c5f884640f 100644 --- a/tests/baselines/reference/computedPropertyNames4_ES5.types +++ b/tests/baselines/reference/computedPropertyNames4_ES5.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { [0]: number; [""]: number; } ->{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [0]: number; [""]: number; } +>v : { [x: string]: number | string; [x: number]: number | string; [0]: number; [""]: number; } +>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: number | string; [x: number]: number | string; [0]: number; [""]: number; } [s]: 0, >s : string diff --git a/tests/baselines/reference/computedPropertyNames4_ES6.types b/tests/baselines/reference/computedPropertyNames4_ES6.types index 1fece561f5a..335c2415bd0 100644 --- a/tests/baselines/reference/computedPropertyNames4_ES6.types +++ b/tests/baselines/reference/computedPropertyNames4_ES6.types @@ -9,8 +9,8 @@ var a: any; >a : any var v = { ->v : { [0]: number; [""]: number; } ->{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [0]: number; [""]: number; } +>v : { [x: string]: number | string; [x: number]: number | string; [0]: number; [""]: number; } +>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: number | string; [x: number]: number | string; [0]: number; [""]: number; } [s]: 0, >s : string diff --git a/tests/baselines/reference/computedPropertyNames7_ES5.types b/tests/baselines/reference/computedPropertyNames7_ES5.types index 8ebd4d75668..3411198b590 100644 --- a/tests/baselines/reference/computedPropertyNames7_ES5.types +++ b/tests/baselines/reference/computedPropertyNames7_ES5.types @@ -6,8 +6,8 @@ enum E { >member : E } var v = { ->v : {} ->{ [E.member]: 0} : {} +>v : { [x: number]: number; } +>{ [E.member]: 0} : { [x: number]: number; } [E.member]: 0 >E.member : E diff --git a/tests/baselines/reference/computedPropertyNames7_ES6.types b/tests/baselines/reference/computedPropertyNames7_ES6.types index 3a78b9c0ec6..eb72d546dbc 100644 --- a/tests/baselines/reference/computedPropertyNames7_ES6.types +++ b/tests/baselines/reference/computedPropertyNames7_ES6.types @@ -6,8 +6,8 @@ enum E { >member : E } var v = { ->v : {} ->{ [E.member]: 0} : {} +>v : { [x: number]: number; } +>{ [E.member]: 0} : { [x: number]: number; } [E.member]: 0 >E.member : E diff --git a/tests/baselines/reference/computedPropertyNamesContextualType1_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType1_ES5.types index bea7267d7d1..09cad594624 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType1_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType1_ES5.types @@ -14,7 +14,7 @@ interface I { var o: I = { >o : I >I : I ->{ ["" + 0](y) { return y.length; }, ["" + 1]: y => y.length} : { [x: string]: (y: string) => number; [x: number]: undefined; } +>{ ["" + 0](y) { return y.length; }, ["" + 1]: y => y.length} : { [x: string]: (y: string) => number; } ["" + 0](y) { return y.length; }, >"" + 0 : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType1_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType1_ES6.types index c8d0be6e833..d9e9970ebec 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType1_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType1_ES6.types @@ -14,7 +14,7 @@ interface I { var o: I = { >o : I >I : I ->{ ["" + 0](y) { return y.length; }, ["" + 1]: y => y.length} : { [x: string]: (y: string) => number; [x: number]: undefined; } +>{ ["" + 0](y) { return y.length; }, ["" + 1]: y => y.length} : { [x: string]: (y: string) => number; } ["" + 0](y) { return y.length; }, >"" + 0 : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType2_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType2_ES5.types index 52de216b803..5a5c48f2536 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType2_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType2_ES5.types @@ -14,7 +14,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: string]: (y: string) => number; [x: number]: (y: string) => number; } +>{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: number]: (y: string) => number; } [+"foo"](y) { return y.length; }, >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType2_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType2_ES6.types index cbbe0edc6a1..330351790ca 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType2_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType2_ES6.types @@ -14,7 +14,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: string]: (y: string) => number; [x: number]: (y: string) => number; } +>{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: number]: (y: string) => number; } [+"foo"](y) { return y.length; }, >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType3_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType3_ES5.types index 5f647fb4c1b..faff27c9154 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType3_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType3_ES5.types @@ -10,7 +10,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: string]: (y: string) => number; } +>{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: number]: (y: string) => number; } [+"foo"](y) { return y.length; }, >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType3_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType3_ES6.types index e872df6f1b2..18c32957944 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType3_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType3_ES6.types @@ -10,7 +10,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: string]: (y: string) => number; } +>{ [+"foo"](y) { return y.length; }, [+"bar"]: y => y.length} : { [x: number]: (y: string) => number; } [+"foo"](y) { return y.length; }, >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType4_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType4_ES5.types index e5a57363ca0..6f75f1a3633 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType4_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType4_ES5.types @@ -12,7 +12,7 @@ interface I { var o: I = { >o : I >I : I ->{ [""+"foo"]: "", [""+"bar"]: 0} : { [x: string]: string | number; [x: number]: undefined; } +>{ [""+"foo"]: "", [""+"bar"]: 0} : { [x: string]: string | number; } [""+"foo"]: "", >""+"foo" : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType4_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType4_ES6.types index bdfa569752b..9ee84176237 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType4_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType4_ES6.types @@ -12,7 +12,7 @@ interface I { var o: I = { >o : I >I : I ->{ [""+"foo"]: "", [""+"bar"]: 0} : { [x: string]: string | number; [x: number]: undefined; } +>{ [""+"foo"]: "", [""+"bar"]: 0} : { [x: string]: string | number; } [""+"foo"]: "", >""+"foo" : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType5_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType5_ES5.types index e142fe937b9..38594f756ab 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType5_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType5_ES5.types @@ -12,7 +12,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"]: "", [+"bar"]: 0} : { [x: string]: string | number; [x: number]: string | number; } +>{ [+"foo"]: "", [+"bar"]: 0} : { [x: number]: string | number; } [+"foo"]: "", >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType5_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType5_ES6.types index 7b385b36770..a07b2e8cd95 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType5_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType5_ES6.types @@ -12,7 +12,7 @@ interface I { var o: I = { >o : I >I : I ->{ [+"foo"]: "", [+"bar"]: 0} : { [x: string]: string | number; [x: number]: string | number; } +>{ [+"foo"]: "", [+"bar"]: 0} : { [x: number]: string | number; } [+"foo"]: "", >+"foo" : number diff --git a/tests/baselines/reference/computedPropertyNamesContextualType6_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType6_ES5.types index a3a393fba0c..b49724ccded 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType6_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType6_ES5.types @@ -19,7 +19,7 @@ declare function foo(obj: I): T foo({ >foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | (() => void) | boolean | number | number[] >foo : (obj: I) => T ->{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; 0: () => void; p: string; } +>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } p: "", >p : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType6_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType6_ES6.types index 4abefe44843..e88959d7cdc 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType6_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType6_ES6.types @@ -19,7 +19,7 @@ declare function foo(obj: I): T foo({ >foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | (() => void) | boolean | number | number[] >foo : (obj: I) => T ->{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; 0: () => void; p: string; } +>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } p: "", >p : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types index ae1004c9819..083388ba975 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types @@ -19,7 +19,7 @@ declare function foo(obj: I): T foo({ >foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : (() => void) | number | number[] >foo : (obj: I) => T ->{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } +>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } p: "", >p : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types index e98e0fb8941..bc6ae72fb4f 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types @@ -19,7 +19,7 @@ declare function foo(obj: I): T foo({ >foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : (() => void) | number | number[] >foo : (obj: I) => T ->{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } +>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; p: string; } p: "", >p : string diff --git a/tests/baselines/reference/computedPropertyNamesContextualType8_ES5.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType8_ES5.errors.txt index 3376243e370..ee36609095a 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType8_ES5.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType8_ES5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; [x: number]: undefined; }' is not assignable to type 'I'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. Index signatures are incompatible. Type 'string | number' is not assignable to type 'boolean'. Type 'string' is not assignable to type 'boolean'. @@ -12,7 +12,7 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualTy var o: I = { ~ -!!! error TS2322: Type '{ [x: string]: string | number; [x: number]: undefined; }' is not assignable to type 'I'. +!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. !!! error TS2322: Index signatures are incompatible. !!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. !!! error TS2322: Type 'string' is not assignable to type 'boolean'. diff --git a/tests/baselines/reference/computedPropertyNamesContextualType8_ES6.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType8_ES6.errors.txt index e4540337ed4..1048fd2e119 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType8_ES6.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType8_ES6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(6,5): error TS2322: Type '{ [x: string]: string | number; [x: number]: undefined; }' is not assignable to type 'I'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. Index signatures are incompatible. Type 'string | number' is not assignable to type 'boolean'. Type 'string' is not assignable to type 'boolean'. @@ -12,7 +12,7 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualTy var o: I = { ~ -!!! error TS2322: Type '{ [x: string]: string | number; [x: number]: undefined; }' is not assignable to type 'I'. +!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. !!! error TS2322: Index signatures are incompatible. !!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. !!! error TS2322: Type 'string' is not assignable to type 'boolean'. diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt index d4085a37e60..e76b3a704dc 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; [x: number]: string | number; }' is not assignable to type 'I'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. Index signatures are incompatible. Type 'string | number' is not assignable to type 'boolean'. Type 'string' is not assignable to type 'boolean'. @@ -12,7 +12,7 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualTy var o: I = { ~ -!!! error TS2322: Type '{ [x: string]: string | number; [x: number]: string | number; }' is not assignable to type 'I'. +!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. !!! error TS2322: Index signatures are incompatible. !!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. !!! error TS2322: Type 'string' is not assignable to type 'boolean'. diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt index eca1360c26f..468ad400c26 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(6,5): error TS2322: Type '{ [x: string]: string | number; [x: number]: string | number; }' is not assignable to type 'I'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. Index signatures are incompatible. Type 'string | number' is not assignable to type 'boolean'. Type 'string' is not assignable to type 'boolean'. @@ -12,7 +12,7 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualTy var o: I = { ~ -!!! error TS2322: Type '{ [x: string]: string | number; [x: number]: string | number; }' is not assignable to type 'I'. +!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. !!! error TS2322: Index signatures are incompatible. !!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. !!! error TS2322: Type 'string' is not assignable to type 'boolean'. diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.js b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.js index e77eb79dc70..f04ea61379d 100644 --- a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.js +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.js @@ -26,4 +26,6 @@ var _a; //// [computedPropertyNamesDeclarationEmit5_ES5.d.ts] -declare var v: {}; +declare var v: { + [x: string]: any; +}; diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.types b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.types index 62faa5c2716..0653059b535 100644 --- a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES5.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5_ES5.ts === var v = { ->v : {} ->{ ["" + ""]: 0, ["" + ""]() { }, get ["" + ""]() { return 0; }, set ["" + ""](x) { }} : {} +>v : { [x: string]: any; } +>{ ["" + ""]: 0, ["" + ""]() { }, get ["" + ""]() { return 0; }, set ["" + ""](x) { }} : { [x: string]: any; } ["" + ""]: 0, >"" + "" : string diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.js b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.js index e19ac43656e..de1f8c65d7f 100644 --- a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.js +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.js @@ -16,4 +16,6 @@ var v = { //// [computedPropertyNamesDeclarationEmit5_ES6.d.ts] -declare var v: {}; +declare var v: { + [x: string]: any; +}; diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.types b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.types index 3eb313d2687..45d1e74f24a 100644 --- a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5_ES6.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5_ES6.ts === var v = { ->v : {} ->{ ["" + ""]: 0, ["" + ""]() { }, get ["" + ""]() { return 0; }, set ["" + ""](x) { }} : {} +>v : { [x: string]: any; } +>{ ["" + ""]: 0, ["" + ""]() { }, get ["" + ""]() { return 0; }, set ["" + ""](x) { }} : { [x: string]: any; } ["" + ""]: 0, >"" + "" : string diff --git a/tests/baselines/reference/generatorTypeCheck41.types b/tests/baselines/reference/generatorTypeCheck41.types index 926aef95ce5..0bed082dc43 100644 --- a/tests/baselines/reference/generatorTypeCheck41.types +++ b/tests/baselines/reference/generatorTypeCheck41.types @@ -3,8 +3,8 @@ function* g() { >g : () => IterableIterator let x = { ->x : {} ->{ [yield 0]: 0 } : {} +>x : { [x: number]: number; } +>{ [yield 0]: 0 } : { [x: number]: number; } [yield 0]: 0 >yield 0 : any diff --git a/tests/baselines/reference/generatorTypeCheck42.types b/tests/baselines/reference/generatorTypeCheck42.types index 855c4697f40..7538b8f2499 100644 --- a/tests/baselines/reference/generatorTypeCheck42.types +++ b/tests/baselines/reference/generatorTypeCheck42.types @@ -3,8 +3,8 @@ function* g() { >g : () => IterableIterator let x = { ->x : {} ->{ [yield 0]() { } } : {} +>x : { [x: number]: () => void; } +>{ [yield 0]() { } } : { [x: number]: () => void; } [yield 0]() { >yield 0 : any diff --git a/tests/baselines/reference/generatorTypeCheck43.types b/tests/baselines/reference/generatorTypeCheck43.types index 50fc7e30c86..a132dfcb015 100644 --- a/tests/baselines/reference/generatorTypeCheck43.types +++ b/tests/baselines/reference/generatorTypeCheck43.types @@ -3,8 +3,8 @@ function* g() { >g : () => IterableIterator let x = { ->x : {} ->{ *[yield 0]() { } } : {} +>x : { [x: number]: () => IterableIterator; } +>{ *[yield 0]() { } } : { [x: number]: () => IterableIterator; } *[yield 0]() { >yield 0 : any diff --git a/tests/baselines/reference/generatorTypeCheck44.types b/tests/baselines/reference/generatorTypeCheck44.types index 2fc67f90556..5c432e41cbe 100644 --- a/tests/baselines/reference/generatorTypeCheck44.types +++ b/tests/baselines/reference/generatorTypeCheck44.types @@ -3,8 +3,8 @@ function* g() { >g : () => IterableIterator let x = { ->x : {} ->{ get [yield 0]() { return 0; } } : {} +>x : { [x: number]: number; } +>{ get [yield 0]() { return 0; } } : { [x: number]: number; } get [yield 0]() { >yield 0 : any diff --git a/tests/baselines/reference/symbolProperty1.types b/tests/baselines/reference/symbolProperty1.types index f39d98853d2..667e51fda8a 100644 --- a/tests/baselines/reference/symbolProperty1.types +++ b/tests/baselines/reference/symbolProperty1.types @@ -3,8 +3,8 @@ var s: symbol; >s : symbol var x = { ->x : {} ->{ [s]: 0, [s]() { }, get [s]() { return 0; }} : {} +>x : { [x: string]: number | (() => void); } +>{ [s]: 0, [s]() { }, get [s]() { return 0; }} : { [x: string]: number | (() => void); } [s]: 0, >s : symbol diff --git a/tests/baselines/reference/symbolProperty2.types b/tests/baselines/reference/symbolProperty2.types index df49130520a..7584c6461e8 100644 --- a/tests/baselines/reference/symbolProperty2.types +++ b/tests/baselines/reference/symbolProperty2.types @@ -5,8 +5,8 @@ var s = Symbol(); >Symbol : SymbolConstructor var x = { ->x : {} ->{ [s]: 0, [s]() { }, get [s]() { return 0; }} : {} +>x : { [x: string]: number | (() => void); } +>{ [s]: 0, [s]() { }, get [s]() { return 0; }} : { [x: string]: number | (() => void); } [s]: 0, >s : symbol diff --git a/tests/baselines/reference/symbolProperty4.types b/tests/baselines/reference/symbolProperty4.types index 93b4417b8ae..7bcf3f23397 100644 --- a/tests/baselines/reference/symbolProperty4.types +++ b/tests/baselines/reference/symbolProperty4.types @@ -1,7 +1,7 @@ === tests/cases/conformance/es6/Symbols/symbolProperty4.ts === var x = { ->x : {} ->{ [Symbol()]: 0, [Symbol()]() { }, get [Symbol()]() { return 0; }} : {} +>x : { [x: string]: number | (() => void); } +>{ [Symbol()]: 0, [Symbol()]() { }, get [Symbol()]() { return 0; }} : { [x: string]: number | (() => void); } [Symbol()]: 0, >Symbol() : symbol From 1af4e1ca64a3ae7b3ca8671ecb95b9d799d464c1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Feb 2016 09:37:14 -0800 Subject: [PATCH 19/55] Removing unused function --- src/compiler/checker.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 611ffb79062..3e8b71b92e6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7843,11 +7843,6 @@ namespace ts { return !!(type.flags & TypeFlags.Union ? forEach((type).types, isTupleLikeType) : isTupleLikeType(type)); } - // Return true if the given contextual type provides an index signature of the given kind - function contextualTypeHasIndexSignature(type: Type, kind: IndexKind): boolean { - return !!(type.flags & TypeFlags.Union ? forEach((type).types, t => getIndexInfoOfStructuredType(t, kind)) : getIndexInfoOfStructuredType(type, kind)); - } - // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one // exists. Otherwise, it is the type of the string index signature in T, if one exists. From 90c08c22015979848bef009298bebd7532737200 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 2 Feb 2016 16:32:10 -0800 Subject: [PATCH 20/55] Port PR#6860 lexically check calling super before this Update baselines add baselines Update baseline Port PR #6860 lexically check calling super before this Check using "super" before "this" lexically instead of using the NodeCheckFlags Remove "type-checking" way of checking if super is used before this. Instead check using whether super occurs before this syntactically Refactor the code Dive down to get super call Address PR Address PR about tests Add a flag so we don't repeatedly finding super call rename function Move tests into correct location Address PR: report error on super call instead of entire constructor node remove marge mark --- src/compiler/checker.ts | 93 +++++++++++++++---- src/compiler/types.ts | 10 +- .../reference/classExtendsNull.errors.txt | 11 +-- .../superCallBeforeThisAccessing1.js | 40 ++++++++ .../superCallBeforeThisAccessing1.symbols | 38 ++++++++ .../superCallBeforeThisAccessing1.types | 43 +++++++++ .../superCallBeforeThisAccessing2.js | 31 +++++++ .../superCallBeforeThisAccessing2.symbols | 23 +++++ .../superCallBeforeThisAccessing2.types | 25 +++++ .../superCallBeforeThisAccessing3.errors.txt | 19 ++++ .../superCallBeforeThisAccessing3.js | 37 ++++++++ .../superCallBeforeThisAccessing4.errors.txt | 24 +++++ .../superCallBeforeThisAccessing4.js | 39 ++++++++ .../superCallBeforeThisAccessing5.js | 22 +++++ .../superCallBeforeThisAccessing5.symbols | 15 +++ .../superCallBeforeThisAccessing5.types | 16 ++++ .../superCallBeforeThisAccessing6.errors.txt | 16 ++++ .../superCallBeforeThisAccessing6.js | 30 ++++++ .../superCallBeforeThisAccessing7.errors.txt | 19 ++++ .../superCallBeforeThisAccessing7.js | 36 +++++++ .../superCallBeforeThisAccessing8.js | 36 +++++++ .../superCallBeforeThisAccessing8.symbols | 32 +++++++ .../superCallBeforeThisAccessing8.types | 34 +++++++ .../superCallBeforeThisAccessing1.ts | 15 +++ .../superCallBeforeThisAccessing2.ts | 9 ++ .../superCallBeforeThisAccessing3.ts | 12 +++ .../superCallBeforeThisAccessing4.ts | 15 +++ .../superCallBeforeThisAccessing5.ts | 6 ++ .../superCallBeforeThisAccessing6.ts | 9 ++ .../superCallBeforeThisAccessing7.ts | 12 +++ .../superCallBeforeThisAccessing8.ts | 12 +++ 31 files changed, 748 insertions(+), 31 deletions(-) create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing1.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing1.symbols create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing1.types create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing2.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing2.symbols create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing2.types create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing3.errors.txt create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing3.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing4.errors.txt create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing4.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing5.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing5.symbols create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing5.types create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing6.errors.txt create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing6.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing7.errors.txt create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing7.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing8.js create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing8.symbols create mode 100644 tests/baselines/reference/superCallBeforeThisAccessing8.types create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 76323f83e08..6508ce755d0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7328,6 +7328,50 @@ namespace ts { } } + function isSuperCallExpression(n: Node): boolean { + return n.kind === SyntaxKind.CallExpression && (n).expression.kind === SyntaxKind.SuperKeyword; + } + + function findFirstSuperCall(n: Node): Node { + if (isSuperCallExpression(n)) { + return n; + } + else if (isFunctionLike(n)) { + return undefined; + } + return forEachChild(n, findFirstSuperCall); + } + + /** + * Return a cached result if super-statement is already found. + * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor + * + * @param constructor constructor-function to look for super statement + */ + function getSuperCallInConstructor(constructor: ConstructorDeclaration): ExpressionStatement { + const links = getNodeLinks(constructor); + + // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result + if (links.hasSuperCall === undefined) { + links.superCall = findFirstSuperCall(constructor.body); + links.hasSuperCall = links.superCall ? true : false; + } + return links.superCall; + } + + /** + * Check if the given class-declaration extends null then return true. + * Otherwise, return false + * @param classDecl a class declaration to check if it extends null + */ + function classDeclarationExtendsNull(classDecl: ClassDeclaration): boolean { + const classSymbol = getSymbolOfNode(classDecl); + const classInstanceType = getDeclaredTypeOfSymbol(classSymbol); + const baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); + + return baseConstructorType === nullType; + } + function checkThisExpression(node: Node): Type { // Stop at the first arrow function so that we can // tell whether 'this' needs to be captured. @@ -7335,10 +7379,25 @@ namespace ts { let needToCaptureLexicalThis = false; if (container.kind === SyntaxKind.Constructor) { - const baseTypeNode = getClassExtendsHeritageClauseElement(container.parent); - if (baseTypeNode && !(getNodeCheckFlags(container) & NodeCheckFlags.HasSeenSuperCall)) { - // In ES6, super inside constructor of class-declaration has to precede "this" accessing - error(node, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + const containingClassDecl = container.parent; + const baseTypeNode = getClassExtendsHeritageClauseElement(containingClassDecl); + + // If a containing class does not have extends clause or the class extends null + // skip checking whether super statement is called before "this" accessing. + if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) { + const superCall = getSuperCallInConstructor(container); + + // We should give an error in the following cases: + // - No super-call + // - "this" is accessing before super-call. + // i.e super(this) + // this.x; super(); + // We want to make sure that super-call is done before accessing "this" so that + // "this" is not accessed as a parameter of the super-call. + if (!superCall || superCall.end > node.pos) { + // In ES6, super inside constructor of class-declaration has to precede "this" accessing + error(node, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + } } } @@ -10287,14 +10346,11 @@ namespace ts { checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); const signature = getResolvedSignature(node); - if (node.expression.kind === SyntaxKind.SuperKeyword) { - const containingFunction = getContainingFunction(node.expression); - if (containingFunction && containingFunction.kind === SyntaxKind.Constructor) { - getNodeLinks(containingFunction).flags |= NodeCheckFlags.HasSeenSuperCall; - } + if (node.expression.kind === SyntaxKind.SuperKeyword) { return voidType; } + if (node.kind === SyntaxKind.NewExpression) { const declaration = signature.declaration; @@ -11875,17 +11931,15 @@ namespace ts { } // TS 1.0 spec (April 2014): 8.3.2 - // Constructors of classes with no extends clause may not contain super calls, whereas - // constructors of derived classes must contain at least one super call somewhere in their function body. + // Constructors of classes with no extends clause and constructors of classes that extends null may not contain super calls, + // whereas constructors of derived classes must contain at least one super call somewhere in their function body. const containingClassDecl = node.parent; if (getClassExtendsHeritageClauseElement(containingClassDecl)) { - const containingClassSymbol = getSymbolOfNode(containingClassDecl); - const containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); - const baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); - - if (containsSuperCall(node.body)) { - if (baseConstructorType === nullType) { - error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); + const classExtendsNull = classDeclarationExtendsNull(containingClassDecl); + const superCall = getSuperCallInConstructor(node); + if (superCall) { + if (classExtendsNull) { + error(superCall, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); } // The first statement in the body of a constructor (excluding prologue directives) must be a super call @@ -11902,6 +11956,7 @@ namespace ts { if (superCallShouldBeFirst) { const statements = (node.body).statements; let superCallStatement: ExpressionStatement; + for (const statement of statements) { if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { superCallStatement = statement; @@ -11916,7 +11971,7 @@ namespace ts { } } } - else if (baseConstructorType !== nullType) { + else if (!classExtendsNull) { error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 31448c2859c..78f441c4b89 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -430,7 +430,6 @@ namespace ts { IntrinsicElement = IntrinsicNamedElement | IntrinsicIndexedElement, } - /* @internal */ export const enum RelationComparisonResult { Succeeded = 1, // Should be truthy @@ -2041,10 +2040,9 @@ namespace ts { LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement - HasSeenSuperCall = 0x00080000, // Set during the binding when encounter 'super' - ClassWithBodyScopedClassBinding = 0x00100000, // Decorated class that contains a binding to itself inside of the class body. - BodyScopedClassBinding = 0x00200000, // Binding to a decorated class inside of the class's body. - NeedsLoopOutParameter = 0x00400000, // Block scoped binding whose value should be explicitly copied outside of the converted loop + ClassWithBodyScopedClassBinding = 0x00080000, // Decorated class that contains a binding to itself inside of the class body. + BodyScopedClassBinding = 0x00100000, // Binding to a decorated class inside of the class's body. + NeedsLoopOutParameter = 0x00200000, // Block scoped binding whose value should be explicitly copied outside of the converted loop } /* @internal */ @@ -2064,6 +2062,8 @@ namespace ts { importOnRightSide?: Symbol; // for import declarations - import that appear on the right side jsxFlags?: JsxFlags; // flags for knowing what kind of element/attributes we're dealing with resolvedJsxType?: Type; // resolved element attributes type of a JSX openinglike element + hasSuperCall?: boolean; // recorded result when we try to find super-call. We only try to find one if this flag is undefined, indicating that we haven't made an attempt. + superCall?: ExpressionStatement; // Cached first super-call found in the constructor. Used in checking whether super is called before this-accessing } export const enum TypeFlags { diff --git a/tests/baselines/reference/classExtendsNull.errors.txt b/tests/baselines/reference/classExtendsNull.errors.txt index 7bb44774826..905c90c42fd 100644 --- a/tests/baselines/reference/classExtendsNull.errors.txt +++ b/tests/baselines/reference/classExtendsNull.errors.txt @@ -1,17 +1,14 @@ -tests/cases/compiler/classExtendsNull.ts(2,5): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' +tests/cases/compiler/classExtendsNull.ts(3,9): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' ==== tests/cases/compiler/classExtendsNull.ts (1 errors) ==== class C extends null { constructor() { - ~~~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ - return Object.create(null); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - } - ~~~~~ + ~~~~~~~ !!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' + return Object.create(null); + } } class D extends null { diff --git a/tests/baselines/reference/superCallBeforeThisAccessing1.js b/tests/baselines/reference/superCallBeforeThisAccessing1.js new file mode 100644 index 00000000000..1fc7ab39c91 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing1.js @@ -0,0 +1,40 @@ +//// [superCallBeforeThisAccessing1.ts] +declare var Factory: any + +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(i); + var s = { + t: this._t + } + var i = Factory.create(s); + } +} + + +//// [superCallBeforeThisAccessing1.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.call(this, i); + var s = { + t: this._t + }; + var i = Factory.create(s); + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing1.symbols b/tests/baselines/reference/superCallBeforeThisAccessing1.symbols new file mode 100644 index 00000000000..5a153728a73 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing1.symbols @@ -0,0 +1,38 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts === +declare var Factory: any +>Factory : Symbol(Factory, Decl(superCallBeforeThisAccessing1.ts, 0, 11)) + +class Base { +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing1.ts, 0, 24)) + + constructor(c) { } +>c : Symbol(c, Decl(superCallBeforeThisAccessing1.ts, 3, 16)) +} +class D extends Base { +>D : Symbol(D, Decl(superCallBeforeThisAccessing1.ts, 4, 1)) +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing1.ts, 0, 24)) + + private _t; +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing1.ts, 5, 22)) + + constructor() { + super(i); +>super : Symbol(Base, Decl(superCallBeforeThisAccessing1.ts, 0, 24)) +>i : Symbol(i, Decl(superCallBeforeThisAccessing1.ts, 12, 11)) + + var s = { +>s : Symbol(s, Decl(superCallBeforeThisAccessing1.ts, 9, 11)) + + t: this._t +>t : Symbol(t, Decl(superCallBeforeThisAccessing1.ts, 9, 17)) +>this._t : Symbol(_t, Decl(superCallBeforeThisAccessing1.ts, 5, 22)) +>this : Symbol(D, Decl(superCallBeforeThisAccessing1.ts, 4, 1)) +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing1.ts, 5, 22)) + } + var i = Factory.create(s); +>i : Symbol(i, Decl(superCallBeforeThisAccessing1.ts, 12, 11)) +>Factory : Symbol(Factory, Decl(superCallBeforeThisAccessing1.ts, 0, 11)) +>s : Symbol(s, Decl(superCallBeforeThisAccessing1.ts, 9, 11)) + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing1.types b/tests/baselines/reference/superCallBeforeThisAccessing1.types new file mode 100644 index 00000000000..e947653584a --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing1.types @@ -0,0 +1,43 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts === +declare var Factory: any +>Factory : any + +class Base { +>Base : Base + + constructor(c) { } +>c : any +} +class D extends Base { +>D : D +>Base : Base + + private _t; +>_t : any + + constructor() { + super(i); +>super(i) : void +>super : typeof Base +>i : any + + var s = { +>s : { t: any; } +>{ t: this._t } : { t: any; } + + t: this._t +>t : any +>this._t : any +>this : this +>_t : any + } + var i = Factory.create(s); +>i : any +>Factory.create(s) : any +>Factory.create : any +>Factory : any +>create : any +>s : { t: any; } + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing2.js b/tests/baselines/reference/superCallBeforeThisAccessing2.js new file mode 100644 index 00000000000..e5acf06bbfd --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing2.js @@ -0,0 +1,31 @@ +//// [superCallBeforeThisAccessing2.ts] +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(() => { this._t }); // no error. only check when this is directly accessing in constructor + } +} + + +//// [superCallBeforeThisAccessing2.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + var _this = this; + _super.call(this, function () { _this._t; }); // no error. only check when this is directly accessing in constructor + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing2.symbols b/tests/baselines/reference/superCallBeforeThisAccessing2.symbols new file mode 100644 index 00000000000..2df2e62354a --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing2.symbols @@ -0,0 +1,23 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts === +class Base { +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing2.ts, 0, 0)) + + constructor(c) { } +>c : Symbol(c, Decl(superCallBeforeThisAccessing2.ts, 1, 16)) +} +class D extends Base { +>D : Symbol(D, Decl(superCallBeforeThisAccessing2.ts, 2, 1)) +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing2.ts, 0, 0)) + + private _t; +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing2.ts, 3, 22)) + + constructor() { + super(() => { this._t }); // no error. only check when this is directly accessing in constructor +>super : Symbol(Base, Decl(superCallBeforeThisAccessing2.ts, 0, 0)) +>this._t : Symbol(_t, Decl(superCallBeforeThisAccessing2.ts, 3, 22)) +>this : Symbol(D, Decl(superCallBeforeThisAccessing2.ts, 2, 1)) +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing2.ts, 3, 22)) + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing2.types b/tests/baselines/reference/superCallBeforeThisAccessing2.types new file mode 100644 index 00000000000..cf0f54b6246 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing2.types @@ -0,0 +1,25 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts === +class Base { +>Base : Base + + constructor(c) { } +>c : any +} +class D extends Base { +>D : D +>Base : Base + + private _t; +>_t : any + + constructor() { + super(() => { this._t }); // no error. only check when this is directly accessing in constructor +>super(() => { this._t }) : void +>super : typeof Base +>() => { this._t } : () => void +>this._t : any +>this : this +>_t : any + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing3.errors.txt b/tests/baselines/reference/superCallBeforeThisAccessing3.errors.txt new file mode 100644 index 00000000000..90b9b034263 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing3.errors.txt @@ -0,0 +1,19 @@ +tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts(9,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts (1 errors) ==== + class Base { + constructor(c) { } + } + class D extends Base { + private _t; + constructor() { + let x = () => { this._t }; + x(); // no error; we only check super is called before this when the container is a constructor + this._t; // error + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(undefined); + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/superCallBeforeThisAccessing3.js b/tests/baselines/reference/superCallBeforeThisAccessing3.js new file mode 100644 index 00000000000..101d74e4bc7 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing3.js @@ -0,0 +1,37 @@ +//// [superCallBeforeThisAccessing3.ts] +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = () => { this._t }; + x(); // no error; we only check super is called before this when the container is a constructor + this._t; // error + super(undefined); + } +} + + +//// [superCallBeforeThisAccessing3.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + var _this = this; + var x = function () { _this._t; }; + x(); // no error; we only check super is called before this when the container is a constructor + this._t; // error + _super.call(this, undefined); + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing4.errors.txt b/tests/baselines/reference/superCallBeforeThisAccessing4.errors.txt new file mode 100644 index 00000000000..91c3e7763f6 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing4.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts(5,9): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' +tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts(12,9): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' + + +==== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts (2 errors) ==== + class D extends null { + private _t; + constructor() { + this._t; + super(); + ~~~~~~~ +!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' + } + } + + class E extends null { + private _t; + constructor() { + super(); + ~~~~~~~ +!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null' + this._t; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/superCallBeforeThisAccessing4.js b/tests/baselines/reference/superCallBeforeThisAccessing4.js new file mode 100644 index 00000000000..9c3c5ab60c4 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing4.js @@ -0,0 +1,39 @@ +//// [superCallBeforeThisAccessing4.ts] +class D extends null { + private _t; + constructor() { + this._t; + super(); + } +} + +class E extends null { + private _t; + constructor() { + super(); + this._t; + } +} + +//// [superCallBeforeThisAccessing4.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var D = (function (_super) { + __extends(D, _super); + function D() { + this._t; + _super.call(this); + } + return D; +}(null)); +var E = (function (_super) { + __extends(E, _super); + function E() { + _super.call(this); + this._t; + } + return E; +}(null)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing5.js b/tests/baselines/reference/superCallBeforeThisAccessing5.js new file mode 100644 index 00000000000..3281a3f8b55 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing5.js @@ -0,0 +1,22 @@ +//// [superCallBeforeThisAccessing5.ts] +class D extends null { + private _t; + constructor() { + this._t; // No error + } +} + + +//// [superCallBeforeThisAccessing5.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var D = (function (_super) { + __extends(D, _super); + function D() { + this._t; // No error + } + return D; +}(null)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing5.symbols b/tests/baselines/reference/superCallBeforeThisAccessing5.symbols new file mode 100644 index 00000000000..a7628e3b8df --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing5.symbols @@ -0,0 +1,15 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts === +class D extends null { +>D : Symbol(D, Decl(superCallBeforeThisAccessing5.ts, 0, 0)) + + private _t; +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing5.ts, 0, 22)) + + constructor() { + this._t; // No error +>this._t : Symbol(_t, Decl(superCallBeforeThisAccessing5.ts, 0, 22)) +>this : Symbol(D, Decl(superCallBeforeThisAccessing5.ts, 0, 0)) +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing5.ts, 0, 22)) + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing5.types b/tests/baselines/reference/superCallBeforeThisAccessing5.types new file mode 100644 index 00000000000..2c0fc33c4dc --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing5.types @@ -0,0 +1,16 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts === +class D extends null { +>D : D +>null : null + + private _t; +>_t : any + + constructor() { + this._t; // No error +>this._t : any +>this : this +>_t : any + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing6.errors.txt b/tests/baselines/reference/superCallBeforeThisAccessing6.errors.txt new file mode 100644 index 00000000000..346b2d58bec --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing6.errors.txt @@ -0,0 +1,16 @@ +tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts(7,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts (1 errors) ==== + class Base { + constructor(c) { } + } + class D extends Base { + private _t; + constructor() { + super(this); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/superCallBeforeThisAccessing6.js b/tests/baselines/reference/superCallBeforeThisAccessing6.js new file mode 100644 index 00000000000..746ce9f2791 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing6.js @@ -0,0 +1,30 @@ +//// [superCallBeforeThisAccessing6.ts] +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(this); + } +} + + +//// [superCallBeforeThisAccessing6.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.call(this, this); + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing7.errors.txt b/tests/baselines/reference/superCallBeforeThisAccessing7.errors.txt new file mode 100644 index 00000000000..4e374fd96ea --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing7.errors.txt @@ -0,0 +1,19 @@ +tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts(8,16): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts (1 errors) ==== + class Base { + constructor(c) { } + } + class D extends Base { + private _t; + constructor() { + let x = { + j: this._t, + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + super(undefined); + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/superCallBeforeThisAccessing7.js b/tests/baselines/reference/superCallBeforeThisAccessing7.js new file mode 100644 index 00000000000..c3aa1655cf7 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing7.js @@ -0,0 +1,36 @@ +//// [superCallBeforeThisAccessing7.ts] +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = { + j: this._t, + } + super(undefined); + } +} + + +//// [superCallBeforeThisAccessing7.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + var x = { + j: this._t + }; + _super.call(this, undefined); + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing8.js b/tests/baselines/reference/superCallBeforeThisAccessing8.js new file mode 100644 index 00000000000..0865c4305a5 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing8.js @@ -0,0 +1,36 @@ +//// [superCallBeforeThisAccessing8.ts] +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = { + k: super(undefined), + j: this._t, // no error + } + } +} + + +//// [superCallBeforeThisAccessing8.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(c) { + } + return Base; +}()); +var D = (function (_super) { + __extends(D, _super); + function D() { + var x = { + k: _super.call(this, undefined), + j: this._t + }; + } + return D; +}(Base)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing8.symbols b/tests/baselines/reference/superCallBeforeThisAccessing8.symbols new file mode 100644 index 00000000000..65341ee9fe3 --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing8.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts === +class Base { +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing8.ts, 0, 0)) + + constructor(c) { } +>c : Symbol(c, Decl(superCallBeforeThisAccessing8.ts, 1, 16)) +} +class D extends Base { +>D : Symbol(D, Decl(superCallBeforeThisAccessing8.ts, 2, 1)) +>Base : Symbol(Base, Decl(superCallBeforeThisAccessing8.ts, 0, 0)) + + private _t; +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing8.ts, 3, 22)) + + constructor() { + let x = { +>x : Symbol(x, Decl(superCallBeforeThisAccessing8.ts, 6, 11)) + + k: super(undefined), +>k : Symbol(k, Decl(superCallBeforeThisAccessing8.ts, 6, 17)) +>super : Symbol(Base, Decl(superCallBeforeThisAccessing8.ts, 0, 0)) +>undefined : Symbol(undefined) + + j: this._t, // no error +>j : Symbol(j, Decl(superCallBeforeThisAccessing8.ts, 7, 32)) +>this._t : Symbol(_t, Decl(superCallBeforeThisAccessing8.ts, 3, 22)) +>this : Symbol(D, Decl(superCallBeforeThisAccessing8.ts, 2, 1)) +>_t : Symbol(_t, Decl(superCallBeforeThisAccessing8.ts, 3, 22)) + } + } +} + diff --git a/tests/baselines/reference/superCallBeforeThisAccessing8.types b/tests/baselines/reference/superCallBeforeThisAccessing8.types new file mode 100644 index 00000000000..dd92d924ceb --- /dev/null +++ b/tests/baselines/reference/superCallBeforeThisAccessing8.types @@ -0,0 +1,34 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts === +class Base { +>Base : Base + + constructor(c) { } +>c : any +} +class D extends Base { +>D : D +>Base : Base + + private _t; +>_t : any + + constructor() { + let x = { +>x : { k: void; j: any; } +>{ k: super(undefined), j: this._t, // no error } : { k: void; j: any; } + + k: super(undefined), +>k : void +>super(undefined) : void +>super : typeof Base +>undefined : undefined + + j: this._t, // no error +>j : any +>this._t : any +>this : this +>_t : any + } + } +} + diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts new file mode 100644 index 00000000000..e5d63f2e930 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing1.ts @@ -0,0 +1,15 @@ +declare var Factory: any + +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(i); + var s = { + t: this._t + } + var i = Factory.create(s); + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts new file mode 100644 index 00000000000..1b0a0d541e9 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing2.ts @@ -0,0 +1,9 @@ +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(() => { this._t }); // no error. only check when this is directly accessing in constructor + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts new file mode 100644 index 00000000000..1386998aaae --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing3.ts @@ -0,0 +1,12 @@ +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = () => { this._t }; + x(); // no error; we only check super is called before this when the container is a constructor + this._t; // error + super(undefined); + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts new file mode 100644 index 00000000000..9135e03b0ce --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing4.ts @@ -0,0 +1,15 @@ +class D extends null { + private _t; + constructor() { + this._t; + super(); + } +} + +class E extends null { + private _t; + constructor() { + super(); + this._t; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts new file mode 100644 index 00000000000..17820277585 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts @@ -0,0 +1,6 @@ +class D extends null { + private _t; + constructor() { + this._t; // No error + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts new file mode 100644 index 00000000000..8f36f2eb056 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing6.ts @@ -0,0 +1,9 @@ +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + super(this); + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts new file mode 100644 index 00000000000..d40c96a60f3 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing7.ts @@ -0,0 +1,12 @@ +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = { + j: this._t, + } + super(undefined); + } +} diff --git a/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts new file mode 100644 index 00000000000..5742cdab845 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing8.ts @@ -0,0 +1,12 @@ +class Base { + constructor(c) { } +} +class D extends Base { + private _t; + constructor() { + let x = { + k: super(undefined), + j: this._t, // no error + } + } +} From 2a674579d3f626471a32dfadee179079a686172d Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Thu, 11 Feb 2016 14:08:18 -0800 Subject: [PATCH 21/55] Remove duplicate function from merging --- src/compiler/checker.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6508ce755d0..fb6a0528c74 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7328,10 +7328,6 @@ namespace ts { } } - function isSuperCallExpression(n: Node): boolean { - return n.kind === SyntaxKind.CallExpression && (n).expression.kind === SyntaxKind.SuperKeyword; - } - function findFirstSuperCall(n: Node): Node { if (isSuperCallExpression(n)) { return n; From 0aad5e5e4554d671cd5efd9979928b4bdc3d8cae Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Thu, 11 Feb 2016 14:29:22 -0800 Subject: [PATCH 22/55] Remove incorrect commment resulting from merging --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fb6a0528c74..dc3704586dd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11927,8 +11927,8 @@ namespace ts { } // TS 1.0 spec (April 2014): 8.3.2 - // Constructors of classes with no extends clause and constructors of classes that extends null may not contain super calls, - // whereas constructors of derived classes must contain at least one super call somewhere in their function body. + // Constructors of classes with no extends clause may not contain super calls, whereas + // constructors of derived classes must contain at least one super call somewhere in their function body. const containingClassDecl = node.parent; if (getClassExtendsHeritageClauseElement(containingClassDecl)) { const classExtendsNull = classDeclarationExtendsNull(containingClassDecl); From a1040f02c4f844d86c791194f1da17e263609c5f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Feb 2016 16:00:20 -0800 Subject: [PATCH 23/55] Fixing comment --- src/compiler/checker.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3e8b71b92e6..50ea133eb67 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6267,6 +6267,7 @@ namespace ts { /** * Return true if type was inferred from an object literal or written as an object type literal + * with no call or construct signatures. */ function isObjectLiteralType(type: Type) { return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral)) !== 0 && From 22979db64cfb53d7d73c001c15b57efbecbe4e99 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Sun, 3 Jan 2016 06:10:47 -0500 Subject: [PATCH 24/55] Add test --- .../typeGuards/typeGuardOfFormFunctionEquality.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts new file mode 100644 index 00000000000..baff942bb8e --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts @@ -0,0 +1,14 @@ +declare function isString1(a: number, b: Object): b is string; + +declare function isString2(a: Object): a is string; + +switch (isString1(0, "")) { + case isString2(""): + default: +} + +var x = isString1(0, "") === isString2(""); + +function isString3(a: number, b: number, c: Object): c is string { + return isString1(0, c); +} From cf13a71af43da3c7b0833066c5218982ccf5f301 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jan 2016 15:06:17 -0800 Subject: [PATCH 25/55] Move type predicates back onto signatures, remove narrowing for property/get type guards. --- src/compiler/checker.ts | 248 ++++++++++++++++++-------------------- src/compiler/types.ts | 19 ++- src/compiler/utilities.ts | 4 + 3 files changed, 130 insertions(+), 141 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6b43b3802c2..9b7b2bd12c8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -131,8 +131,8 @@ namespace ts { const noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - const anySignature = createSignature(undefined, undefined, emptyArray, anyType, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); - const unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); + const anySignature = createSignature(undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); + const unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false); const enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); @@ -1711,6 +1711,15 @@ namespace ts { return result; } + function typePredicateToString(typePredicate: TypePredicate, enclosingDeclaration?: Declaration, flags?: TypeFormatFlags): string { + const writer = getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildTypePredicateDisplay(typePredicate, writer, enclosingDeclaration, flags); + const result = writer.string(); + releaseStringWriter(writer); + + return result; + } + function getTypeAliasForTypeLiteral(type: Type): Symbol { if (type.symbol && type.symbol.flags & SymbolFlags.TypeLiteral) { let node = type.symbol.declarations[0].parent; @@ -1844,16 +1853,10 @@ namespace ts { function writeType(type: Type, flags: TypeFormatFlags) { // Write undefined/null type as any if (type.flags & TypeFlags.Intrinsic) { - if (type.flags & TypeFlags.PredicateType) { - buildTypePredicateDisplay(writer, (type as PredicateType).predicate); - buildTypeDisplay((type as PredicateType).predicate.type, writer, enclosingDeclaration, flags, symbolStack); - } - else { - // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving - writer.writeKeyword(!(globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike) && isTypeAny(type) - ? "any" - : (type).intrinsicName); - } + // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving + writer.writeKeyword(!(globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike) && isTypeAny(type) + ? "any" + : (type).intrinsicName); } else if (type.flags & TypeFlags.ThisType) { if (inObjectTypeLiteral) { @@ -2213,7 +2216,7 @@ namespace ts { writePunctuation(writer, SyntaxKind.CloseParenToken); } - function buildTypePredicateDisplay(writer: SymbolWriter, predicate: TypePredicate) { + function buildTypePredicateDisplay(predicate: TypePredicate, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, symbolStack?: Symbol[]): void { if (isIdentifierTypePredicate(predicate)) { writer.writeParameter(predicate.parameterName); } @@ -2223,6 +2226,7 @@ namespace ts { writeSpace(writer); writeKeyword(writer, SyntaxKind.IsKeyword); writeSpace(writer); + buildTypeDisplay(predicate.type, writer, enclosingDeclaration, flags, symbolStack); } function buildReturnTypeDisplay(signature: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, symbolStack?: Symbol[]) { @@ -2235,8 +2239,13 @@ namespace ts { } writeSpace(writer); - const returnType = getReturnTypeOfSignature(signature); - buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); + if (signature.typePredicate) { + buildTypePredicateDisplay(signature.typePredicate, writer, enclosingDeclaration, flags, symbolStack); + } + else { + const returnType = getReturnTypeOfSignature(signature); + buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); + } } function buildSignatureDisplay(signature: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind, symbolStack?: Symbol[]) { @@ -2255,6 +2264,7 @@ namespace ts { } buildDisplayForParametersAndDelimiters(signature.parameters, writer, enclosingDeclaration, flags, symbolStack); + buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack); } @@ -2262,6 +2272,7 @@ namespace ts { buildSymbolDisplay, buildTypeDisplay, buildTypeParameterDisplay, + buildTypePredicateDisplay, buildParameterDisplay, buildDisplayForParametersAndDelimiters, buildDisplayForTypeParametersAndDelimiters, @@ -2800,9 +2811,6 @@ namespace ts { if (declaration.kind === SyntaxKind.PropertyAssignment) { return type; } - if (type.flags & TypeFlags.PredicateType && (declaration.kind === SyntaxKind.PropertyDeclaration || declaration.kind === SyntaxKind.PropertySignature)) { - return type; - } return getWidenedType(type); } @@ -3533,12 +3541,13 @@ namespace ts { } function createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], parameters: Symbol[], - resolvedReturnType: Type, minArgumentCount: number, hasRestParameter: boolean, hasStringLiterals: boolean): Signature { + resolvedReturnType: Type, typePredicate: TypePredicate, minArgumentCount: number, hasRestParameter: boolean, hasStringLiterals: boolean): Signature { const sig = new Signature(checker); sig.declaration = declaration; sig.typeParameters = typeParameters; sig.parameters = parameters; sig.resolvedReturnType = resolvedReturnType; + sig.typePredicate = typePredicate; sig.minArgumentCount = minArgumentCount; sig.hasRestParameter = hasRestParameter; sig.hasStringLiterals = hasStringLiterals; @@ -3547,14 +3556,14 @@ namespace ts { function cloneSignature(sig: Signature): Signature { return createSignature(sig.declaration, sig.typeParameters, sig.parameters, sig.resolvedReturnType, - sig.minArgumentCount, sig.hasRestParameter, sig.hasStringLiterals); + sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasStringLiterals); } function getDefaultConstructSignatures(classType: InterfaceType): Signature[] { const baseConstructorType = getBaseConstructorTypeOfClass(classType); const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct); if (baseSignatures.length === 0) { - return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false)]; + return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasStringLiterals*/ false)]; } const baseTypeNode = getBaseTypeNodeOfClass(classType); const typeArguments = map(baseTypeNode.typeArguments, getTypeFromTypeNode); @@ -4086,6 +4095,7 @@ namespace ts { let minArgumentCount = -1; const isJSConstructSignature = isJSDocConstructSignature(declaration); let returnType: Type = undefined; + let typePredicate: TypePredicate = undefined; // If this is a JSDoc construct signature, then skip the first parameter in the // parameter list. The first parameter represents the return type of the construct @@ -4129,6 +4139,9 @@ namespace ts { } else if (declaration.type) { returnType = getTypeFromTypeNode(declaration.type); + if (declaration.type.kind === SyntaxKind.TypePredicate) { + typePredicate = createTypePredicateFromTypePredicateNode(declaration.type as TypePredicateNode); + } } else { if (declaration.flags & NodeFlags.JavaScriptFile) { @@ -4150,7 +4163,7 @@ namespace ts { } } - links.resolvedSignature = createSignature(declaration, typeParameters, parameters, returnType, minArgumentCount, hasRestParameter(declaration), hasStringLiterals); + links.resolvedSignature = createSignature(declaration, typeParameters, parameters, returnType, typePredicate, minArgumentCount, hasRestParameter(declaration), hasStringLiterals); } return links.resolvedSignature; } @@ -4862,25 +4875,6 @@ namespace ts { return links.resolvedType; } - function getPredicateType(node: TypePredicateNode): Type { - return createPredicateType(getSymbolOfNode(node), createTypePredicateFromTypePredicateNode(node)); - } - - function createPredicateType(symbol: Symbol, predicate: ThisTypePredicate | IdentifierTypePredicate) { - const type = createType(TypeFlags.Boolean | TypeFlags.PredicateType) as PredicateType; - type.symbol = symbol; - type.predicate = predicate; - return type; - } - - function getTypeFromPredicateTypeNode(node: TypePredicateNode): Type { - const links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getPredicateType(node); - } - return links.resolvedType; - } - function getTypeFromTypeNode(node: TypeNode): Type { switch (node.kind) { case SyntaxKind.AnyKeyword: @@ -4905,7 +4899,7 @@ namespace ts { case SyntaxKind.JSDocTypeReference: return getTypeFromTypeReference(node); case SyntaxKind.TypePredicate: - return getTypeFromPredicateTypeNode(node); + return booleanType; case SyntaxKind.ExpressionWithTypeArguments: return getTypeFromTypeReference(node); case SyntaxKind.TypeQuery: @@ -5057,6 +5051,7 @@ namespace ts { function instantiateSignature(signature: Signature, mapper: TypeMapper, eraseTypeParameters?: boolean): Signature { let freshTypeParameters: TypeParameter[]; + let freshTypePredicate: TypePredicate; if (signature.typeParameters && !eraseTypeParameters) { // First create a fresh set of type parameters, then include a mapping from the old to the // new type parameters in the mapper function. Finally store this mapper in the new type @@ -5067,9 +5062,13 @@ namespace ts { tp.mapper = mapper; } } + if (signature.typePredicate) { + freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); + } const result = createSignature(signature.declaration, freshTypeParameters, instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), + freshTypePredicate, signature.minArgumentCount, signature.hasRestParameter, signature.hasStringLiterals); result.target = signature; result.mapper = mapper; @@ -5139,10 +5138,6 @@ namespace ts { if (type.flags & TypeFlags.Intersection) { return getIntersectionType(instantiateList((type).types, mapper, instantiateType)); } - if (type.flags & TypeFlags.PredicateType) { - const predicate = (type as PredicateType).predicate; - return createPredicateType(type.symbol, cloneTypePredicate(predicate, mapper)); - } } return type; } @@ -5288,21 +5283,58 @@ namespace ts { const sourceReturnType = getReturnTypeOfSignature(source); // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions - if (targetReturnType.flags & TypeFlags.PredicateType && (targetReturnType as PredicateType).predicate.kind === TypePredicateKind.Identifier) { - if (!(sourceReturnType.flags & TypeFlags.PredicateType)) { + if (target.typePredicate) { + if (source.typePredicate) { + result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes); + } + else if (isIdentifierTypePredicate(target.typePredicate)) { if (reportErrors) { errorReporter(Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); } return Ternary.False; } } + else { + result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); + } - result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); } return result; } + function compareTypePredicateRelatedTo(source: TypePredicate, + target: TypePredicate, + reportErrors: boolean, + errorReporter: (d: DiagnosticMessage, arg0?: string, arg1?: string) => void, + compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary { + if (source.kind !== target.kind) { + if (reportErrors) { + errorReporter(Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); + errorReporter(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return Ternary.False; + } + + if (source.kind === TypePredicateKind.Identifier) { + const sourceIdentifierPredicate = source as IdentifierTypePredicate; + const targetIdentifierPredicate = target as IdentifierTypePredicate; + if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { + if (reportErrors) { + errorReporter(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); + errorReporter(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return Ternary.False; + } + } + + const related = compareTypes(source.type, target.type, reportErrors); + if (related === Ternary.False && reportErrors) { + errorReporter(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return related; + } + function isImplementationCompatibleWithOverload(implementation: Signature, overload: Signature): boolean { const erasedSource = getErasedSignature(implementation); const erasedTarget = getErasedSignature(overload); @@ -5429,33 +5461,6 @@ namespace ts { if (source === numberType && target.flags & TypeFlags.Enum) return Ternary.True; } if (source.flags & TypeFlags.Boolean && target.flags & TypeFlags.Boolean) { - if (source.flags & TypeFlags.PredicateType && target.flags & TypeFlags.PredicateType) { - const sourcePredicate = source as PredicateType; - const targetPredicate = target as PredicateType; - if (sourcePredicate.predicate.kind !== targetPredicate.predicate.kind) { - if (reportErrors) { - reportError(Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); - reportError(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typeToString(source), typeToString(target)); - } - return Ternary.False; - } - if (sourcePredicate.predicate.kind === TypePredicateKind.Identifier) { - const sourceIdentifierPredicate = sourcePredicate.predicate as IdentifierTypePredicate; - const targetIdentifierPredicate = targetPredicate.predicate as IdentifierTypePredicate; - if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { - if (reportErrors) { - reportError(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); - reportError(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typeToString(source), typeToString(target)); - } - return Ternary.False; - } - } - const related = isRelatedTo(sourcePredicate.predicate.type, targetPredicate.predicate.type, reportErrors, headMessage); - if (related === Ternary.False && reportErrors) { - reportError(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typeToString(source), typeToString(target)); - } - return related; - } return Ternary.True; } @@ -6311,9 +6316,6 @@ namespace ts { if (type.flags & (TypeFlags.Undefined | TypeFlags.Null)) { return anyType; } - if (type.flags & TypeFlags.PredicateType) { - return booleanType; - } if (type.flags & TypeFlags.ObjectLiteral) { return getWidenedTypeOfObjectLiteral(type); } @@ -6541,11 +6543,6 @@ namespace ts { inferFromTypes(sourceTypes[i], targetTypes[i]); } } - else if (source.flags & TypeFlags.PredicateType && target.flags & TypeFlags.PredicateType) { - if ((source as PredicateType).predicate.kind === (target as PredicateType).predicate.kind) { - inferFromTypes((source as PredicateType).predicate.type, (target as PredicateType).predicate.type); - } - } else if (source.flags & TypeFlags.Tuple && target.flags & TypeFlags.Tuple && (source).elementTypes.length === (target).elementTypes.length) { // If source and target are tuples of the same size, infer from element types const sourceTypes = (source).elementTypes; @@ -6641,7 +6638,13 @@ namespace ts { function inferFromSignature(source: Signature, target: Signature) { forEachMatchingParameterType(source, target, inferFromTypes); - inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + + if (source.typePredicate && target.typePredicate && source.typePredicate.kind === target.typePredicate.kind) { + inferFromTypes(source.typePredicate.type, target.typePredicate.type); + } + else { + inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + } } function inferFromIndexTypes(source: Type, target: Type, sourceKind: IndexKind, targetKind: IndexKind) { @@ -7067,46 +7070,33 @@ namespace ts { return originalType; } - function narrowTypeByTypePredicate(type: Type, expr: CallExpression, assumeTrue: boolean): Type { + function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { if (type.flags & TypeFlags.Any) { return type; } - const signature = getResolvedSignature(expr); - const predicateType = getReturnTypeOfSignature(signature); - - if (!predicateType || !(predicateType.flags & TypeFlags.PredicateType)) { + const signature = getResolvedSignature(callExpression); + + const predicate = signature.typePredicate; + if (!predicate) { return type; } - const predicate = (predicateType as PredicateType).predicate; + if (isIdentifierTypePredicate(predicate)) { - const callExpression = expr as CallExpression; if (callExpression.arguments[predicate.parameterIndex] && getSymbolAtTypePredicatePosition(callExpression.arguments[predicate.parameterIndex]) === symbol) { return getNarrowedType(type, predicate.type, assumeTrue); } } else { - const expression = skipParenthesizedNodes(expr.expression); - return narrowTypeByThisTypePredicate(type, predicate, expression, assumeTrue); + const invokedExpression = skipParenthesizedNodes(callExpression.expression); + return narrowTypeByThisTypePredicate(type, predicate, invokedExpression, assumeTrue); } return type; } - function narrowTypeByTypePredicateMember(type: Type, expr: ElementAccessExpression | PropertyAccessExpression, assumeTrue: boolean): Type { - if (type.flags & TypeFlags.Any) { - return type; - } - const memberType = getTypeOfExpression(expr); - if (!(memberType.flags & TypeFlags.PredicateType)) { - return type; - } - - return narrowTypeByThisTypePredicate(type, (memberType as PredicateType).predicate as ThisTypePredicate, expr, assumeTrue); - } - - function narrowTypeByThisTypePredicate(type: Type, predicate: ThisTypePredicate, expression: Expression, assumeTrue: boolean): Type { - if (expression.kind === SyntaxKind.ElementAccessExpression || expression.kind === SyntaxKind.PropertyAccessExpression) { - const accessExpression = expression as ElementAccessExpression | PropertyAccessExpression; + function narrowTypeByThisTypePredicate(type: Type, predicate: ThisTypePredicate, invokedExpression: Expression, assumeTrue: boolean): Type { + if (invokedExpression.kind === SyntaxKind.ElementAccessExpression || invokedExpression.kind === SyntaxKind.PropertyAccessExpression) { + const accessExpression = invokedExpression as ElementAccessExpression | PropertyAccessExpression; const possibleIdentifier = skipParenthesizedNodes(accessExpression.expression); if (possibleIdentifier.kind === SyntaxKind.Identifier && getSymbolAtTypePredicatePosition(possibleIdentifier) === symbol) { return getNarrowedType(type, predicate.type, assumeTrue); @@ -7121,6 +7111,7 @@ namespace ts { case SyntaxKind.Identifier: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.QualifiedName: + // TODO (drosen): Why a qualified name? return getSymbolOfEntityNameOrPropertyAccessExpression(expr as Node as (EntityName | PropertyAccessExpression)); } } @@ -7153,9 +7144,6 @@ namespace ts { return narrowType(type, (expr).operand, !assumeTrue); } break; - case SyntaxKind.ElementAccessExpression: - case SyntaxKind.PropertyAccessExpression: - return narrowTypeByTypePredicateMember(type, expr as (ElementAccessExpression | PropertyAccessExpression), assumeTrue); } return type; } @@ -10544,12 +10532,13 @@ namespace ts { return aggregatedTypes; } - /* - *TypeScript Specification 1.0 (6.3) - July 2014 - * An explicitly typed function whose return type isn't the Void type, - * the Any type, or a union type containing the Void or Any type as a constituent - * must have at least one return statement somewhere in its body. - * An exception to this rule is if the function implementation consists of a single 'throw' statement. + /** + * TypeScript Specification 1.0 (6.3) - July 2014 + * An explicitly typed function whose return type isn't the Void type, + * the Any type, or a union type containing the Void or Any type as a constituent + * must have at least one return statement somewhere in its body. + * An exception to this rule is if the function implementation consists of a single 'throw' statement. + * * @param returnType - return type of the function, can be undefined if return type is not explicitly specified */ function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func: FunctionLikeDeclaration, returnType: Type): void { @@ -11579,16 +11568,16 @@ namespace ts { if (!parent) { return; } - const returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(parent)); - if (!returnType || !(returnType.flags & TypeFlags.PredicateType)) { + const typePredicate = getSignatureFromDeclaration(parent).typePredicate; + if (!typePredicate) { return; } + const { parameterName } = node; - if (parameterName.kind === SyntaxKind.ThisType) { + if (isThisTypePredicate(typePredicate)) { getTypeFromThisTypeNode(parameterName as ThisTypeNode); } else { - const typePredicate = (returnType).predicate; if (typePredicate.parameterIndex >= 0) { if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) { error(parameterName, @@ -11603,12 +11592,8 @@ namespace ts { else if (parameterName) { let hasReportedError = false; for (const { name } of parent.parameters) { - if ((name.kind === SyntaxKind.ObjectBindingPattern || - name.kind === SyntaxKind.ArrayBindingPattern) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern( - name, - parameterName, - typePredicate.parameterName)) { + if (isBindingPattern(name) && + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, parameterName, typePredicate.parameterName)) { hasReportedError = true; break; } @@ -11676,8 +11661,9 @@ namespace ts { forEach(node.parameters, checkParameter); - checkSourceElement(node.type); - + if (node.type) { + checkSourceElement(node.type); + } if (produceDiagnostics) { checkCollisionWithArgumentsInGeneratedCode(node); @@ -13634,7 +13620,7 @@ namespace ts { error(node.expression, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } - else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func) || returnType.flags & TypeFlags.PredicateType) { + else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) { if (isAsyncFunctionLike(func)) { const promisedType = getPromisedType(returnType); const awaitedType = checkAwaitedType(exprType, node.expression, Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 6b9f49cac01..7f88879c75f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1749,6 +1749,7 @@ namespace ts { buildSignatureDisplay(signatures: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): void; buildParameterDisplay(parameter: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; buildTypeParameterDisplay(tp: TypeParameter, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; + buildTypePredicateDisplay(predicate: TypePredicate, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; buildTypeParameterDisplayFromSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; buildDisplayForParametersAndDelimiters(parameters: Symbol[], writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; buildDisplayForTypeParametersAndDelimiters(typeParameters: TypeParameter[], writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void; @@ -1814,22 +1815,24 @@ namespace ts { Identifier } - export interface TypePredicate { + export interface TypePredicateBase { kind: TypePredicateKind; type: Type; } // @kind (TypePredicateKind.This) - export interface ThisTypePredicate extends TypePredicate { + export interface ThisTypePredicate extends TypePredicateBase { _thisTypePredicateBrand: any; } // @kind (TypePredicateKind.Identifier) - export interface IdentifierTypePredicate extends TypePredicate { + export interface IdentifierTypePredicate extends TypePredicateBase { parameterName: string; parameterIndex: number; } + export type TypePredicate = IdentifierTypePredicate | ThisTypePredicate; + /* @internal */ export type AnyImportSyntax = ImportDeclaration | ImportEqualsDeclaration; @@ -2095,7 +2098,6 @@ namespace ts { ESSymbol = 0x01000000, // Type of symbol primitive introduced in ES6 ThisType = 0x02000000, // This type ObjectLiteralPatternWithComputedProperties = 0x04000000, // Object literal type implied by binding pattern has computed properties - PredicateType = 0x08000000, // Predicate types are also Boolean types, but should not be considered Intrinsics - there's no way to capture this with flags /* @internal */ Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null, @@ -2107,7 +2109,7 @@ namespace ts { UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, /* @internal */ - RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral | PredicateType, + RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType } @@ -2128,11 +2130,6 @@ namespace ts { intrinsicName: string; // Name of intrinsic type } - // Predicate types (TypeFlags.Predicate) - export interface PredicateType extends Type { - predicate: ThisTypePredicate | IdentifierTypePredicate; - } - // String literal types (TypeFlags.StringLiteral) export interface StringLiteralType extends Type { text: string; // Text of string literal @@ -2267,6 +2264,8 @@ namespace ts { erasedSignatureCache?: Signature; // Erased version of signature (deferred) /* @internal */ isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison + /* @internal */ + typePredicate?: TypePredicate; } export const enum IndexKind { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0903d937d7b..4df8d723f5e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -746,6 +746,10 @@ namespace ts { return predicate && predicate.kind === TypePredicateKind.Identifier; } + export function isThisTypePredicate(predicate: TypePredicate): predicate is ThisTypePredicate { + return predicate && predicate.kind === TypePredicateKind.This; + } + export function getContainingFunction(node: Node): FunctionLikeDeclaration { while (true) { node = node.parent; From 7c7e2aa032e6312a0ca4dcb3f2d7870153ca7298 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jan 2016 17:23:56 -0800 Subject: [PATCH 26/55] Error on nodes which should not have type predicates. --- src/compiler/checker.ts | 5 ++++- src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9b7b2bd12c8..3ee1f2c6b72 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11563,11 +11563,14 @@ namespace ts { return -1; } - function checkTypePredicate(node: TypePredicateNode) { + function checkTypePredicate(node: TypePredicateNode): void { const parent = getTypePredicateParent(node); if (!parent) { + // The parent must not be valid. + error(node.parent, Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); return; } + const typePredicate = getSignatureFromDeclaration(parent).typePredicate; if (!typePredicate) { return; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 25b86e67d34..2c8de840fc0 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -723,6 +723,10 @@ "category": "Error", "code": 1227 }, + "A type predicate is only allowed in return type position for functions and methods.": { + "category": "Error", + "code": 1228 + }, "A type predicate cannot reference a rest parameter.": { "category": "Error", "code": 1229 From ec0eabb2e9843a654c5e07ddf100f4e8df2a9a1f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jan 2016 17:25:40 -0800 Subject: [PATCH 27/55] Minor rename. --- src/compiler/parser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 66d33f9d7d9..ee9eee6a62b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1937,7 +1937,7 @@ namespace ts { return finishNode(node); } - function parseTypePredicate(lhs: Identifier | ThisTypeNode): TypePredicateNode { + function parseThisTypePredicate(lhs: Identifier | ThisTypeNode): TypePredicateNode { nextToken(); const node = createNode(SyntaxKind.TypePredicate, lhs.pos) as TypePredicateNode; node.parameterName = lhs; @@ -2362,7 +2362,7 @@ namespace ts { case SyntaxKind.ThisKeyword: { const thisKeyword = parseThisTypeNode(); if (token === SyntaxKind.IsKeyword && !scanner.hasPrecedingLineBreak()) { - return parseTypePredicate(thisKeyword); + return parseThisTypePredicate(thisKeyword); } else { return thisKeyword; From 050f52f07de838c75fcaaafe6ac3165da883e50a Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jan 2016 18:00:23 -0800 Subject: [PATCH 28/55] Added tests. --- .../typeGuards/typePredicateOnVariableDeclaration01.ts | 3 +++ .../typeGuards/typePredicateOnVariableDeclaration02.ts | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts diff --git a/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts b/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts new file mode 100644 index 00000000000..a445e18ee40 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts @@ -0,0 +1,3 @@ +// @declaration: true + +var x: this is string; \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts b/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts new file mode 100644 index 00000000000..4b010287337 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts @@ -0,0 +1,3 @@ +// @declaration: true + +var y: z is number; \ No newline at end of file From b191a00b1c7fb2ce6c5adbb6213e808d446ce500 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jan 2016 18:04:48 -0800 Subject: [PATCH 29/55] Actually, it makes more sense to error on the predicate annotation than anything else. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3ee1f2c6b72..dcaa5f97d78 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11567,7 +11567,7 @@ namespace ts { const parent = getTypePredicateParent(node); if (!parent) { // The parent must not be valid. - error(node.parent, Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); + error(node, Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); return; } From ab5bc714a5169a3218b3ce2ed3a941c21af9f9eb Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Jan 2016 16:58:19 -0800 Subject: [PATCH 30/55] Removed trailing whitespace for linter. --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dcaa5f97d78..e90623e24f8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7075,7 +7075,7 @@ namespace ts { return type; } const signature = getResolvedSignature(callExpression); - + const predicate = signature.typePredicate; if (!predicate) { return type; From ba392403cc125cf2b3702b21b76521e02753d551 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 29 Jan 2016 17:03:16 -0800 Subject: [PATCH 31/55] Fixed up fourslash tests to only test functions. --- .../thisPredicateFunctionCompletions01.ts | 52 ++++++++ .../thisPredicateFunctionCompletions02.ts | 43 +++++++ ... => thisPredicateFunctionCompletions03.ts} | 0 .../thisPredicateFunctionQuickInfo01.ts | 59 +++++++++ .../thisPredicateFunctionQuickInfo02.ts | 57 +++++++++ .../thisPredicateMemberCompletions.ts | 95 -------------- .../fourslash/thisPredicateMemberQuickInfo.ts | 117 ------------------ 7 files changed, 211 insertions(+), 212 deletions(-) create mode 100644 tests/cases/fourslash/thisPredicateFunctionCompletions01.ts create mode 100644 tests/cases/fourslash/thisPredicateFunctionCompletions02.ts rename tests/cases/fourslash/{thisPredicateFunctionCompletions.ts => thisPredicateFunctionCompletions03.ts} (100%) create mode 100644 tests/cases/fourslash/thisPredicateFunctionQuickInfo01.ts create mode 100644 tests/cases/fourslash/thisPredicateFunctionQuickInfo02.ts delete mode 100644 tests/cases/fourslash/thisPredicateMemberCompletions.ts delete mode 100644 tests/cases/fourslash/thisPredicateMemberQuickInfo.ts diff --git a/tests/cases/fourslash/thisPredicateFunctionCompletions01.ts b/tests/cases/fourslash/thisPredicateFunctionCompletions01.ts new file mode 100644 index 00000000000..8c9abb85418 --- /dev/null +++ b/tests/cases/fourslash/thisPredicateFunctionCompletions01.ts @@ -0,0 +1,52 @@ +/// + +//// class FileSystemObject { +//// isFile(): this is Item { +//// return this instanceof Item; +//// } +//// isDirectory(): this is Directory { +//// return this instanceof Directory; +//// } +//// isNetworked(): this is (Networked & this) { +//// return !!(this as Networked).host; +//// } +//// constructor(public path: string) {} +//// } +//// +//// class Item extends FileSystemObject { +//// constructor(path: string, public content: string) { super(path); } +//// } +//// class Directory extends FileSystemObject { +//// children: FileSystemObject[]; +//// } +//// interface Networked { +//// host: string; +//// } +//// +//// const obj: FileSystemObject = new Item("/foo", ""); +//// if (obj.isFile()) { +//// obj./*1*/; +//// if (obj.isNetworked()) { +//// obj./*2*/; +//// } +//// } +//// if (obj.isDirectory()) { +//// obj./*3*/; +//// if (obj.isNetworked()) { +//// obj./*4*/; +//// } +//// } +//// if (obj.isNetworked()) { +//// obj./*5*/; +//// } + +goTo.marker("1"); +verify.completionListContains("content"); +goTo.marker("2"); +verify.completionListContains("host"); +goTo.marker("3"); +verify.completionListContains("children"); +goTo.marker("4"); +verify.completionListContains("host"); +goTo.marker("5"); +verify.completionListContains("host"); \ No newline at end of file diff --git a/tests/cases/fourslash/thisPredicateFunctionCompletions02.ts b/tests/cases/fourslash/thisPredicateFunctionCompletions02.ts new file mode 100644 index 00000000000..e2e311ea649 --- /dev/null +++ b/tests/cases/fourslash/thisPredicateFunctionCompletions02.ts @@ -0,0 +1,43 @@ +/// + +//// interface Sundries { +//// broken: boolean; +//// } +//// +//// interface Supplies { +//// spoiled: boolean; +//// } +//// +//// interface Crate { +//// contents: T; +//// isSundries(): this is Crate; +//// isSupplies(): this is Crate; +//// isPackedTight(): this is (this & {extraContents: T}); +//// } +//// const crate: Crate; +//// if (crate.isPackedTight()) { +//// crate./*1*/; +//// } +//// if (crate.isSundries()) { +//// crate.contents./*2*/; +//// if (crate.isPackedTight()) { +//// crate./*3*/; +//// } +//// } +//// if (crate.isSupplies()) { +//// crate.contents./*4*/; +//// if (crate.isPackedTight()) { +//// crate./*5*/; +//// } +//// } + +goTo.marker("1"); +verify.completionListContains("extraContents"); +goTo.marker("2"); +verify.completionListContains("broken"); +goTo.marker("3"); +verify.completionListContains("extraContents"); +goTo.marker("4"); +verify.completionListContains("spoiled"); +goTo.marker("5"); +verify.completionListContains("extraContents"); \ No newline at end of file diff --git a/tests/cases/fourslash/thisPredicateFunctionCompletions.ts b/tests/cases/fourslash/thisPredicateFunctionCompletions03.ts similarity index 100% rename from tests/cases/fourslash/thisPredicateFunctionCompletions.ts rename to tests/cases/fourslash/thisPredicateFunctionCompletions03.ts diff --git a/tests/cases/fourslash/thisPredicateFunctionQuickInfo01.ts b/tests/cases/fourslash/thisPredicateFunctionQuickInfo01.ts new file mode 100644 index 00000000000..135df559189 --- /dev/null +++ b/tests/cases/fourslash/thisPredicateFunctionQuickInfo01.ts @@ -0,0 +1,59 @@ +/// + +//// class FileSystemObject { +//// /*1*/isFile(): this is Item { +//// return this instanceof Item; +//// } +//// /*2*/isDirectory(): this is Directory { +//// return this instanceof Directory; +//// } +//// /*3*/isNetworked(): this is (Networked & this) { +//// return !!(this as Networked).host; +//// } +//// constructor(public path: string) {} +//// } +//// +//// class Item extends FileSystemObject { +//// constructor(path: string, public content: string) { super(path); } +//// } +//// class Directory extends FileSystemObject { +//// children: FileSystemObject[]; +//// } +//// interface Networked { +//// host: string; +//// } +//// +//// const obj: FileSystemObject = new Item("/foo", ""); +//// if (obj.isFile/*4*/()) { +//// obj.; +//// if (obj.isNetworked/*5*/()) { +//// obj.; +//// } +//// } +//// if (obj.isDirectory/*6*/()) { +//// obj.; +//// if (obj.isNetworked/*7*/()) { +//// obj.; +//// } +//// } +//// if (obj.isNetworked/*8*/()) { +//// obj.; +//// } + +goTo.marker("1"); +verify.quickInfoIs("(method) FileSystemObject.isFile(): this is Item"); +goTo.marker("2"); +verify.quickInfoIs("(method) FileSystemObject.isDirectory(): this is Directory"); +goTo.marker("3"); +verify.quickInfoIs("(method) FileSystemObject.isNetworked(): this is Networked & this"); + +goTo.marker("4"); +verify.quickInfoIs("(method) FileSystemObject.isFile(): this is Item"); +goTo.marker("5"); +verify.quickInfoIs("(method) FileSystemObject.isNetworked(): this is Networked & Item"); +goTo.marker("6"); +verify.quickInfoIs("(method) FileSystemObject.isDirectory(): this is Directory"); +goTo.marker("7"); +verify.quickInfoIs("(method) FileSystemObject.isNetworked(): this is Networked & Directory"); +goTo.marker("8"); +verify.quickInfoIs("(method) FileSystemObject.isNetworked(): this is Networked & FileSystemObject"); \ No newline at end of file diff --git a/tests/cases/fourslash/thisPredicateFunctionQuickInfo02.ts b/tests/cases/fourslash/thisPredicateFunctionQuickInfo02.ts new file mode 100644 index 00000000000..b8135026c82 --- /dev/null +++ b/tests/cases/fourslash/thisPredicateFunctionQuickInfo02.ts @@ -0,0 +1,57 @@ +/// + +//// interface Sundries { +//// broken: boolean; +//// } +//// +//// interface Supplies { +//// spoiled: boolean; +//// } +//// +//// interface Crate { +//// contents: T; +//// /*1*/isSundries(): this is Crate; +//// /*2*/isSupplies(): this is Crate; +//// /*3*/isPackedTight(): this is (this & {extraContents: T}); +//// } +//// const crate: Crate; +//// if (crate.isPackedTight/*4*/()) { +//// crate.; +//// } +//// if (crate.isSundries/*5*/()) { +//// crate.contents.; +//// if (crate.isPackedTight/*6*/()) { +//// crate.; +//// } +//// } +//// if (crate.isSupplies/*7*/()) { +//// crate.contents.; +//// if (crate.isPackedTight/*8*/()) { +//// crate.; +//// } +//// } + +goTo.marker("1"); +verify.quickInfoIs("(method) Crate.isSundries(): this is Crate"); +goTo.marker("2"); +verify.quickInfoIs("(method) Crate.isSupplies(): this is Crate"); +goTo.marker("3"); +verify.quickInfoIs(`(method) Crate.isPackedTight(): this is this & { + extraContents: T; +}`); +goTo.marker("4"); +verify.quickInfoIs(`(method) Crate.isPackedTight(): this is Crate & { + extraContents: any; +}`); +goTo.marker("5"); +verify.quickInfoIs("(method) Crate.isSundries(): this is Crate"); +goTo.marker("6"); +verify.quickInfoIs(`(method) Crate.isPackedTight(): this is Crate & { + extraContents: Sundries; +}`); +goTo.marker("7"); +verify.quickInfoIs("(method) Crate.isSupplies(): this is Crate"); +goTo.marker("8"); +verify.quickInfoIs(`(method) Crate.isPackedTight(): this is Crate & { + extraContents: Supplies; +}`); \ No newline at end of file diff --git a/tests/cases/fourslash/thisPredicateMemberCompletions.ts b/tests/cases/fourslash/thisPredicateMemberCompletions.ts deleted file mode 100644 index 24ce742faac..00000000000 --- a/tests/cases/fourslash/thisPredicateMemberCompletions.ts +++ /dev/null @@ -1,95 +0,0 @@ -/// - -//// class FileSystemObject { -//// get is/*1*/File(): this is Item { -//// return this instanceof Item; -//// } -//// set is/*2*/File(param) { -//// // noop -//// } -//// get is/*3*/Directory(): this is Directory { -//// return this instanceof Directory; -//// } -//// is/*4*/Networked: this is (Networked & this); -//// constructor(public path: string) {} -//// } -//// -//// class Item extends FileSystemObject { -//// constructor(path: string, public content: string) { super(path); } -//// } -//// class Directory extends FileSystemObject { -//// children: FileSystemObject[]; -//// } -//// interface Networked { -//// host: string; -//// } -//// -//// interface Sundries { -//// broken: boolean; -//// } -//// -//// interface Supplies { -//// spoiled: boolean; -//// } -//// -//// interface Crate { -//// contents: T; -//// is/*5*/Sundries: this is Crate; -//// is/*6*/Supplies: this is Crate; -//// is/*7*/PackedTight: this is (this & {extraContents: T}); -//// } -//// -//// const obj: FileSystemObject = new Item("/foo", ""); -//// if (obj.is/*8*/File) { -//// obj./*9*/; -//// if (obj.is/*10*/Networked) { -//// obj./*11*/; -//// } -//// } -//// if (obj.is/*12*/Directory) { -//// obj./*13*/; -//// if (obj.is/*14*/Networked) { -//// obj./*15*/; -//// } -//// } -//// if (obj.is/*16*/Networked) { -//// obj./*17*/; -//// } -//// -//// const crate: Crate; -//// if (crate.is/*18*/PackedTight) { -//// crate./*19*/; -//// } -//// if (crate.is/*20*/Sundries) { -//// crate.contents./*21*/; -//// if (crate.is/*22*/PackedTight) { -//// crate./*23*/ -//// } -//// } -//// if (crate.is/*24*/Supplies) { -//// crate.contents./*25*/; -//// if (crate.is/*26*/PackedTight) { -//// crate./*27*/ -//// } -//// } - -goTo.marker("9"); -verify.completionListContains("content"); -goTo.marker("11"); -verify.completionListContains("host"); -goTo.marker("13"); -verify.completionListContains("children"); -goTo.marker("15"); -verify.completionListContains("host"); -goTo.marker("17"); -verify.completionListContains("host"); -goTo.marker("19"); -verify.completionListContains("extraContents"); -goTo.marker("21"); -verify.completionListContains("broken"); -goTo.marker("23"); -verify.completionListContains("extraContents"); -goTo.marker("25"); -verify.completionListContains("spoiled"); -goTo.marker("27"); -verify.completionListContains("extraContents"); \ No newline at end of file diff --git a/tests/cases/fourslash/thisPredicateMemberQuickInfo.ts b/tests/cases/fourslash/thisPredicateMemberQuickInfo.ts deleted file mode 100644 index 20d519e0008..00000000000 --- a/tests/cases/fourslash/thisPredicateMemberQuickInfo.ts +++ /dev/null @@ -1,117 +0,0 @@ -/// - -//// class FileSystemObject { -//// get is/*1*/File(): this is Item { -//// return this instanceof Item; -//// } -//// set is/*2*/File(param) { -//// // noop -//// } -//// get is/*3*/Directory(): this is Directory { -//// return this instanceof Directory; -//// } -//// is/*4*/Networked: this is (Networked & this); -//// constructor(public path: string) {} -//// } -//// -//// class Item extends FileSystemObject { -//// constructor(path: string, public content: string) { super(path); } -//// } -//// class Directory extends FileSystemObject { -//// children: FileSystemObject[]; -//// } -//// interface Networked { -//// host: string; -//// } -//// -//// interface Sundries { -//// broken: boolean; -//// } -//// -//// interface Supplies { -//// spoiled: boolean; -//// } -//// -//// interface Crate { -//// contents: T; -//// is/*5*/Sundries: this is Crate; -//// is/*6*/Supplies: this is Crate; -//// is/*7*/PackedTight: this is (this & {extraContents: T}); -//// } -//// -//// const obj: FileSystemObject = new Item("/foo", ""); -//// if (obj.is/*8*/File) { -//// obj./*9*/; -//// if (obj.is/*10*/Networked) { -//// obj./*11*/; -//// } -//// } -//// if (obj.is/*12*/Directory) { -//// obj./*13*/; -//// if (obj.is/*14*/Networked) { -//// obj./*15*/; -//// } -//// } -//// if (obj.is/*16*/Networked) { -//// obj./*17*/; -//// } -//// -//// const crate: Crate; -//// if (crate.is/*18*/PackedTight) { -//// crate./*19*/; -//// } -//// if (crate.is/*20*/Sundries) { -//// crate.contents./*21*/; -//// if (crate.is/*22*/PackedTight) { -//// crate./*23*/ -//// } -//// } -//// if (crate.is/*24*/Supplies) { -//// crate.contents./*25*/; -//// if (crate.is/*26*/PackedTight) { -//// crate./*27*/ -//// } -//// } - -goTo.marker("1"); -verify.quickInfoIs("(property) FileSystemObject.isFile: this is Item"); -goTo.marker("2"); -verify.quickInfoIs("(property) FileSystemObject.isFile: this is Item"); -goTo.marker("3"); -verify.quickInfoIs("(property) FileSystemObject.isDirectory: this is Directory"); -goTo.marker("4"); -verify.quickInfoIs("(property) FileSystemObject.isNetworked: this is Networked & this"); -goTo.marker("5"); -verify.quickInfoIs("(property) Crate.isSundries: this is Crate"); -goTo.marker("6"); -verify.quickInfoIs("(property) Crate.isSupplies: this is Crate"); -goTo.marker("7"); -verify.quickInfoIs(`(property) Crate.isPackedTight: this is this & { - extraContents: T; -}`); -goTo.marker("8"); -verify.quickInfoIs("(property) FileSystemObject.isFile: this is Item"); -goTo.marker("10"); -verify.quickInfoIs("(property) FileSystemObject.isNetworked: this is Networked & Item"); -goTo.marker("12"); -verify.quickInfoIs("(property) FileSystemObject.isDirectory: this is Directory"); -goTo.marker("14"); -verify.quickInfoIs("(property) FileSystemObject.isNetworked: this is Networked & Directory"); -goTo.marker("16"); -verify.quickInfoIs("(property) FileSystemObject.isNetworked: this is Networked & FileSystemObject"); -goTo.marker("18"); -verify.quickInfoIs(`(property) Crate.isPackedTight: this is Crate & { - extraContents: any; -}`); -goTo.marker("20"); -verify.quickInfoIs("(property) Crate.isSundries: this is Crate"); -goTo.marker("22"); -verify.quickInfoIs(`(property) Crate.isPackedTight: this is Crate & { - extraContents: Sundries; -}`); -goTo.marker("24"); -verify.quickInfoIs("(property) Crate.isSupplies: this is Crate"); -goTo.marker("26"); -verify.quickInfoIs(`(property) Crate.isPackedTight: this is Crate & { - extraContents: Supplies; -}`); \ No newline at end of file From f944d3e997792d21b2162cd0e2507fe85d4298d5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 10 Feb 2016 15:19:27 -0800 Subject: [PATCH 32/55] Addressed CR feedback. --- src/compiler/checker.ts | 4 +--- src/compiler/parser.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e90623e24f8..2fc3245b701 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7110,9 +7110,7 @@ namespace ts { switch (expr.kind) { case SyntaxKind.Identifier: case SyntaxKind.PropertyAccessExpression: - case SyntaxKind.QualifiedName: - // TODO (drosen): Why a qualified name? - return getSymbolOfEntityNameOrPropertyAccessExpression(expr as Node as (EntityName | PropertyAccessExpression)); + return getSymbolOfEntityNameOrPropertyAccessExpression(expr as (Identifier | PropertyAccessExpression)); } } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ee9eee6a62b..65fec19126b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1937,7 +1937,7 @@ namespace ts { return finishNode(node); } - function parseThisTypePredicate(lhs: Identifier | ThisTypeNode): TypePredicateNode { + function parseThisTypePredicate(lhs: ThisTypeNode): TypePredicateNode { nextToken(); const node = createNode(SyntaxKind.TypePredicate, lhs.pos) as TypePredicateNode; node.parameterName = lhs; From 1e2760696ecc00cefa7c8db16cf6af03107f08e4 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 12 Feb 2016 14:12:21 -0800 Subject: [PATCH 33/55] Added tests for declaration emit. --- .../declarationEmitIdentifierPredicates01.ts | 6 ++++++ ...onEmitIdentifierPredicatesWithPrivateName01.ts | 10 ++++++++++ .../declarationEmitThisPredicates01.ts | 11 +++++++++++ .../declarationEmitThisPredicates02.ts | 15 +++++++++++++++ ...larationEmitThisPredicatesWithPrivateName01.ts | 11 +++++++++++ ...larationEmitThisPredicatesWithPrivateName02.ts | 15 +++++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts create mode 100644 tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts new file mode 100644 index 00000000000..c4a223f07ef --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts @@ -0,0 +1,6 @@ +// @declaration: true +// @module: commonjs + +export function f(x: any): x is number { + return typeof x === "number"; +} \ No newline at end of file diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts new file mode 100644 index 00000000000..1398b2bc040 --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts @@ -0,0 +1,10 @@ +// @declaration: true +// @module: commonjs + +interface I { + a: number; +} + +export function f(x: any): x is I { + return typeof x.a === "number"; +} \ No newline at end of file diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts new file mode 100644 index 00000000000..69af9c5b077 --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts @@ -0,0 +1,11 @@ +// @declaration: true +// @module: commonjs + +export class C { + m(): this is D { + return this instanceof D; + } +} + +export class D extends C { +} \ No newline at end of file diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts new file mode 100644 index 00000000000..02f2a798831 --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts @@ -0,0 +1,15 @@ +// @declaration: true +// @module: commonjs + +export interface Foo { + a: string; + b: number; + c: boolean; +} + +export const obj = { + m(): this is Foo { + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts new file mode 100644 index 00000000000..461c7d17571 --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts @@ -0,0 +1,11 @@ +// @declaration: true +// @module: commonjs + +export class C { + m(): this is D { + return this instanceof D; + } +} + +class D extends C { +} \ No newline at end of file diff --git a/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts new file mode 100644 index 00000000000..c238bb16ec8 --- /dev/null +++ b/tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts @@ -0,0 +1,15 @@ +// @declaration: true +// @module: commonjs + +interface Foo { + a: string; + b: number; + c: boolean; +} + +export const obj = { + m(): this is Foo { + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } +} \ No newline at end of file From 1e849f895ce82a439a08563dd5a32d0492e8ffa3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 12 Feb 2016 16:28:48 -0800 Subject: [PATCH 34/55] Accepted baselines. --- .../arrayBufferIsViewNarrowsType.types | 2 +- ...larationEmitIdentifierPredicates01.symbols | 10 + ...eclarationEmitIdentifierPredicates01.types | 13 + ...fierPredicatesWithPrivateName01.errors.txt | 14 + .../declarationEmitThisPredicates01.symbols | 19 ++ .../declarationEmitThisPredicates01.types | 20 ++ ...declarationEmitThisPredicates02.errors.txt | 19 ++ ...ThisPredicatesWithPrivateName01.errors.txt | 15 ++ ...ThisPredicatesWithPrivateName02.errors.txt | 22 ++ tests/baselines/reference/isArray.types | 2 +- .../stringLiteralCheckedInIf02.types | 2 +- .../stringLiteralTypesAsTags01.types | 4 +- .../stringLiteralTypesAsTags02.types | 4 +- .../stringLiteralTypesAsTags03.types | 4 +- .../stringLiteralTypesTypePredicates01.types | 4 +- .../reference/typeGuardFunction.types | 14 +- .../typeGuardFunctionErrors.errors.txt | 18 +- .../reference/typeGuardFunctionGenerics.types | 4 +- .../typeGuardFunctionOfFormThis.types | 36 +-- .../typeGuardOfFormFunctionEquality.symbols | 41 +++ .../typeGuardOfFormFunctionEquality.types | 54 ++++ .../reference/typeGuardOfFormIsType.types | 16 +- .../typeGuardOfFormIsTypeOnInterfaces.types | 16 +- .../typeGuardOfFormThisMember.errors.txt | 130 +++++++++ .../typeGuardOfFormThisMember.symbols | 240 ----------------- .../reference/typeGuardOfFormThisMember.types | 254 ------------------ ...typeGuardOfFormThisMemberErrors.errors.txt | 30 +-- ...redicateOnVariableDeclaration01.errors.txt | 8 + ...redicateOnVariableDeclaration02.errors.txt | 20 ++ .../unionAndIntersectionInference1.types | 4 +- 30 files changed, 469 insertions(+), 570 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitIdentifierPredicates01.symbols create mode 100644 tests/baselines/reference/declarationEmitIdentifierPredicates01.types create mode 100644 tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.errors.txt create mode 100644 tests/baselines/reference/declarationEmitThisPredicates01.symbols create mode 100644 tests/baselines/reference/declarationEmitThisPredicates01.types create mode 100644 tests/baselines/reference/declarationEmitThisPredicates02.errors.txt create mode 100644 tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.errors.txt create mode 100644 tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.errors.txt create mode 100644 tests/baselines/reference/typeGuardOfFormFunctionEquality.symbols create mode 100644 tests/baselines/reference/typeGuardOfFormFunctionEquality.types create mode 100644 tests/baselines/reference/typeGuardOfFormThisMember.errors.txt delete mode 100644 tests/baselines/reference/typeGuardOfFormThisMember.symbols delete mode 100644 tests/baselines/reference/typeGuardOfFormThisMember.types create mode 100644 tests/baselines/reference/typePredicateOnVariableDeclaration01.errors.txt create mode 100644 tests/baselines/reference/typePredicateOnVariableDeclaration02.errors.txt diff --git a/tests/baselines/reference/arrayBufferIsViewNarrowsType.types b/tests/baselines/reference/arrayBufferIsViewNarrowsType.types index b9d4f3db81b..129b7d601d8 100644 --- a/tests/baselines/reference/arrayBufferIsViewNarrowsType.types +++ b/tests/baselines/reference/arrayBufferIsViewNarrowsType.types @@ -4,7 +4,7 @@ var obj: Object; >Object : Object if (ArrayBuffer.isView(obj)) { ->ArrayBuffer.isView(obj) : arg is ArrayBufferView +>ArrayBuffer.isView(obj) : boolean >ArrayBuffer.isView : (arg: any) => arg is ArrayBufferView >ArrayBuffer : ArrayBufferConstructor >isView : (arg: any) => arg is ArrayBufferView diff --git a/tests/baselines/reference/declarationEmitIdentifierPredicates01.symbols b/tests/baselines/reference/declarationEmitIdentifierPredicates01.symbols new file mode 100644 index 00000000000..eb8b1151ceb --- /dev/null +++ b/tests/baselines/reference/declarationEmitIdentifierPredicates01.symbols @@ -0,0 +1,10 @@ +=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts === + +export function f(x: any): x is number { +>f : Symbol(f, Decl(declarationEmitIdentifierPredicates01.ts, 0, 0)) +>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18)) +>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18)) + + return typeof x === "number"; +>x : Symbol(x, Decl(declarationEmitIdentifierPredicates01.ts, 1, 18)) +} diff --git a/tests/baselines/reference/declarationEmitIdentifierPredicates01.types b/tests/baselines/reference/declarationEmitIdentifierPredicates01.types new file mode 100644 index 00000000000..7d8a5668265 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIdentifierPredicates01.types @@ -0,0 +1,13 @@ +=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicates01.ts === + +export function f(x: any): x is number { +>f : (x: any) => x is number +>x : any +>x : any + + return typeof x === "number"; +>typeof x === "number" : boolean +>typeof x : string +>x : any +>"number" : string +} diff --git a/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.errors.txt b/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.errors.txt new file mode 100644 index 00000000000..e9084c45490 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts(6,33): error TS4060: Return type of exported function has or is using private name 'I'. + + +==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateName01.ts (1 errors) ==== + + interface I { + a: number; + } + + export function f(x: any): x is I { + ~ +!!! error TS4060: Return type of exported function has or is using private name 'I'. + return typeof x.a === "number"; + } \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitThisPredicates01.symbols b/tests/baselines/reference/declarationEmitThisPredicates01.symbols new file mode 100644 index 00000000000..d57f937d6c5 --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicates01.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts === + +export class C { +>C : Symbol(C, Decl(declarationEmitThisPredicates01.ts, 0, 0)) + + m(): this is D { +>m : Symbol(m, Decl(declarationEmitThisPredicates01.ts, 1, 16)) +>D : Symbol(D, Decl(declarationEmitThisPredicates01.ts, 5, 1)) + + return this instanceof D; +>this : Symbol(C, Decl(declarationEmitThisPredicates01.ts, 0, 0)) +>D : Symbol(D, Decl(declarationEmitThisPredicates01.ts, 5, 1)) + } +} + +export class D extends C { +>D : Symbol(D, Decl(declarationEmitThisPredicates01.ts, 5, 1)) +>C : Symbol(C, Decl(declarationEmitThisPredicates01.ts, 0, 0)) +} diff --git a/tests/baselines/reference/declarationEmitThisPredicates01.types b/tests/baselines/reference/declarationEmitThisPredicates01.types new file mode 100644 index 00000000000..9d801a40965 --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicates01.types @@ -0,0 +1,20 @@ +=== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates01.ts === + +export class C { +>C : C + + m(): this is D { +>m : () => this is D +>D : D + + return this instanceof D; +>this instanceof D : boolean +>this : this +>D : typeof D + } +} + +export class D extends C { +>D : D +>C : C +} diff --git a/tests/baselines/reference/declarationEmitThisPredicates02.errors.txt b/tests/baselines/reference/declarationEmitThisPredicates02.errors.txt new file mode 100644 index 00000000000..4d95cf01136 --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicates02.errors.txt @@ -0,0 +1,19 @@ +tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts(9,10): error TS2526: A 'this' type is available only in a non-static member of a class or interface. + + +==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts (1 errors) ==== + + export interface Foo { + a: string; + b: number; + c: boolean; + } + + export const obj = { + m(): this is Foo { + ~~~~ +!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface. + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.errors.txt b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.errors.txt new file mode 100644 index 00000000000..67c76283f80 --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.errors.txt @@ -0,0 +1,15 @@ +tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts(3,18): error TS4055: Return type of public method from exported class has or is using private name 'D'. + + +==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName01.ts (1 errors) ==== + + export class C { + m(): this is D { + ~ +!!! error TS4055: Return type of public method from exported class has or is using private name 'D'. + return this instanceof D; + } + } + + class D extends C { + } \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.errors.txt b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.errors.txt new file mode 100644 index 00000000000..86c0f478133 --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.errors.txt @@ -0,0 +1,22 @@ +tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts(8,14): error TS4025: Exported variable 'obj' has or is using private name 'Foo'. +tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts(9,10): error TS2526: A 'this' type is available only in a non-static member of a class or interface. + + +==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts (2 errors) ==== + + interface Foo { + a: string; + b: number; + c: boolean; + } + + export const obj = { + ~~~ +!!! error TS4025: Exported variable 'obj' has or is using private name 'Foo'. + m(): this is Foo { + ~~~~ +!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface. + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/isArray.types b/tests/baselines/reference/isArray.types index de54e9d064c..bc452b12bef 100644 --- a/tests/baselines/reference/isArray.types +++ b/tests/baselines/reference/isArray.types @@ -4,7 +4,7 @@ var maybeArray: number | number[]; if (Array.isArray(maybeArray)) { ->Array.isArray(maybeArray) : arg is any[] +>Array.isArray(maybeArray) : boolean >Array.isArray : (arg: any) => arg is any[] >Array : ArrayConstructor >isArray : (arg: any) => arg is any[] diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.types b/tests/baselines/reference/stringLiteralCheckedInIf02.types index f91eea03097..79f4c6a223a 100644 --- a/tests/baselines/reference/stringLiteralCheckedInIf02.types +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.types @@ -31,7 +31,7 @@ function f(foo: T) { >T : ("a" | "b")[] | "a" | "b" if (isS(foo)) { ->isS(foo) : t is "a" | "b" +>isS(foo) : boolean >isS : (t: ("a" | "b")[] | "a" | "b") => t is "a" | "b" >foo : ("a" | "b")[] | "a" | "b" diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.types b/tests/baselines/reference/stringLiteralTypesAsTags01.types index 6966ede5482..a7f403e76ec 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags01.types +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.types @@ -88,7 +88,7 @@ let x: A = { } if (hasKind(x, "A")) { ->hasKind(x, "A") : entity is A +>hasKind(x, "A") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; (entity: Entity, kind: "A" | "B"): entity is Entity; } >x : A >"A" : "A" @@ -105,7 +105,7 @@ else { if (!hasKind(x, "B")) { >!hasKind(x, "B") : boolean ->hasKind(x, "B") : entity is B +>hasKind(x, "B") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; (entity: Entity, kind: "A" | "B"): entity is Entity; } >x : A >"B" : "B" diff --git a/tests/baselines/reference/stringLiteralTypesAsTags02.types b/tests/baselines/reference/stringLiteralTypesAsTags02.types index 0b8ea0faf67..edad220b086 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags02.types +++ b/tests/baselines/reference/stringLiteralTypesAsTags02.types @@ -82,7 +82,7 @@ let x: A = { } if (hasKind(x, "A")) { ->hasKind(x, "A") : entity is A +>hasKind(x, "A") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; } >x : A >"A" : "A" @@ -99,7 +99,7 @@ else { if (!hasKind(x, "B")) { >!hasKind(x, "B") : boolean ->hasKind(x, "B") : entity is B +>hasKind(x, "B") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; } >x : A >"B" : "B" diff --git a/tests/baselines/reference/stringLiteralTypesAsTags03.types b/tests/baselines/reference/stringLiteralTypesAsTags03.types index a1d83a1c058..25816659388 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags03.types +++ b/tests/baselines/reference/stringLiteralTypesAsTags03.types @@ -85,7 +85,7 @@ let x: A = { } if (hasKind(x, "A")) { ->hasKind(x, "A") : entity is A +>hasKind(x, "A") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; } >x : A >"A" : "A" @@ -102,7 +102,7 @@ else { if (!hasKind(x, "B")) { >!hasKind(x, "B") : boolean ->hasKind(x, "B") : entity is B +>hasKind(x, "B") : boolean >hasKind : { (entity: Entity, kind: "A"): entity is A; (entity: Entity, kind: "B"): entity is B; } >x : A >"B" : "B" diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.types b/tests/baselines/reference/stringLiteralTypesTypePredicates01.types index 43c8271b76b..41da80afd30 100644 --- a/tests/baselines/reference/stringLiteralTypesTypePredicates01.types +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.types @@ -36,7 +36,7 @@ var x: Kind = "A"; >"A" : "A" if (kindIs(x, "A")) { ->kindIs(x, "A") : kind is "A" +>kindIs(x, "A") : boolean >kindIs : { (kind: "A" | "B", is: "A"): kind is "A"; (kind: "A" | "B", is: "B"): kind is "B"; } >x : "A" | "B" >"A" : "A" @@ -53,7 +53,7 @@ else { if (!kindIs(x, "B")) { >!kindIs(x, "B") : boolean ->kindIs(x, "B") : kind is "B" +>kindIs(x, "B") : boolean >kindIs : { (kind: "A" | "B", is: "A"): kind is "A"; (kind: "A" | "B", is: "B"): kind is "B"; } >x : "A" | "B" >"B" : "B" diff --git a/tests/baselines/reference/typeGuardFunction.types b/tests/baselines/reference/typeGuardFunction.types index 9bab1e7ca2c..50a5fcaf324 100644 --- a/tests/baselines/reference/typeGuardFunction.types +++ b/tests/baselines/reference/typeGuardFunction.types @@ -54,7 +54,7 @@ var b: B; // Basic if (isC(a)) { ->isC(a) : p1 is C +>isC(a) : boolean >isC : (p1: any) => p1 is C >a : A @@ -70,7 +70,7 @@ var subType: C; >C : C if(isA(subType)) { ->isA(subType) : p1 is A +>isA(subType) : boolean >isA : (p1: any) => p1 is A >subType : C @@ -87,7 +87,7 @@ var union: A | B; >B : B if(isA(union)) { ->isA(union) : p1 is A +>isA(union) : boolean >isA : (p1: any) => p1 is A >union : A | B @@ -118,7 +118,7 @@ declare function isC_multipleParams(p1, p2): p1 is C; >C : C if (isC_multipleParams(a, 0)) { ->isC_multipleParams(a, 0) : p1 is C +>isC_multipleParams(a, 0) : boolean >isC_multipleParams : (p1: any, p2: any) => p1 is C >a : A >0 : number @@ -197,7 +197,7 @@ declare function acceptingBoolean(a: boolean); acceptingBoolean(isA(a)); >acceptingBoolean(isA(a)) : any >acceptingBoolean : (a: boolean) => any ->isA(a) : p1 is A +>isA(a) : boolean >isA : (p1: any) => p1 is A >a : A @@ -223,8 +223,8 @@ let union2: C | B; let union3: boolean | B = isA(union2) || union2; >union3 : boolean | B >B : B ->isA(union2) || union2 : p1 is A | B ->isA(union2) : p1 is A +>isA(union2) || union2 : boolean | B +>isA(union2) : boolean >isA : (p1: any) => p1 is A >union2 : C | B >union2 : B diff --git a/tests/baselines/reference/typeGuardFunctionErrors.errors.txt b/tests/baselines/reference/typeGuardFunctionErrors.errors.txt index 9ecad007672..8e4bf651518 100644 --- a/tests/baselines/reference/typeGuardFunctionErrors.errors.txt +++ b/tests/baselines/reference/typeGuardFunctionErrors.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(2,7): error TS2300: Duplicate identifier 'A'. -tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(15,12): error TS2322: Type 'string' is not assignable to type 'x is A'. +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(15,12): error TS2322: Type 'string' is not assignable to type 'boolean'. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(18,55): error TS2304: Cannot find name 'x'. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(18,57): error TS1144: '{' or ';' expected. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(18,57): error TS2304: Cannot find name 'is'. @@ -43,9 +43,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(98,22) tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(98,22): error TS2304: Cannot find name 'is'. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(98,25): error TS1005: ';' expected. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(98,27): error TS1005: ';' expected. +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(104,25): error TS1228: A type predicate is only allowed in return type position for functions and methods. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(105,16): error TS2322: Type 'boolean' is not assignable to type 'D'. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(105,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(107,20): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(110,20): error TS1228: A type predicate is only allowed in return type position for functions and methods. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(111,16): error TS2408: Setters cannot return a value. +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(116,18): error TS1228: A type predicate is only allowed in return type position for functions and methods. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(120,22): error TS2304: Cannot find name 'p1'. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(120,25): error TS1005: ';' expected. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(120,25): error TS2304: Cannot find name 'is'. @@ -57,7 +61,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(133,34 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern. -==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (50 errors) ==== +==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (54 errors) ==== class A { ~ @@ -76,7 +80,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39 function hasANonBooleanReturnStatement(x): x is A { return ''; ~~ -!!! error TS2322: Type 'string' is not assignable to type 'x is A'. +!!! error TS2322: Type 'string' is not assignable to type 'boolean'. } function hasTypeGuardTypeInsideTypeGuardType(x): x is x is A { @@ -245,6 +249,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39 // Non-compatiable type predicate positions for signature declarations class D { constructor(p1: A): p1 is C { + ~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. return true; ~~~~ !!! error TS2322: Type 'boolean' is not assignable to type 'D'. @@ -252,9 +258,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39 !!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class } get m1(p1: A): p1 is C { + ~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. return true; } set m2(p1: A): p1 is C { + ~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. return true; ~~~~ !!! error TS2408: Setters cannot return a value. @@ -263,6 +273,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39 interface I1 { new (p1: A): p1 is C; + ~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. } interface I2 { diff --git a/tests/baselines/reference/typeGuardFunctionGenerics.types b/tests/baselines/reference/typeGuardFunctionGenerics.types index 1f2ad2b69be..c4655e71f0c 100644 --- a/tests/baselines/reference/typeGuardFunctionGenerics.types +++ b/tests/baselines/reference/typeGuardFunctionGenerics.types @@ -100,7 +100,7 @@ let test1: boolean = funA(isB); >isB : (p1: any) => p1 is B if (funB(retC, a)) { ->funB(retC, a) : p2 is C +>funB(retC, a) : boolean >funB : (p1: (p1: any) => T, p2: any) => p2 is T >retC : (x: any) => C >a : A @@ -118,7 +118,7 @@ let test2: B = funC(isB); >isB : (p1: any) => p1 is B if (funD(isC, a)) { ->funD(isC, a) : p2 is C +>funD(isC, a) : boolean >funD : (p1: (p1: any) => p1 is T, p2: any) => p2 is T >isC : (p1: any) => p1 is C >a : A diff --git a/tests/baselines/reference/typeGuardFunctionOfFormThis.types b/tests/baselines/reference/typeGuardFunctionOfFormThis.types index e91c77dd07a..6b21fea3457 100644 --- a/tests/baselines/reference/typeGuardFunctionOfFormThis.types +++ b/tests/baselines/reference/typeGuardFunctionOfFormThis.types @@ -45,7 +45,7 @@ let a: RoyalGuard = new FollowerGuard(); >FollowerGuard : typeof FollowerGuard if (a.isLeader()) { ->a.isLeader() : this is LeadGuard +>a.isLeader() : boolean >a.isLeader : () => this is LeadGuard >a : RoyalGuard >isLeader : () => this is LeadGuard @@ -57,7 +57,7 @@ if (a.isLeader()) { >lead : () => void } else if (a.isFollower()) { ->a.isFollower() : this is FollowerGuard +>a.isFollower() : boolean >a.isFollower : () => this is FollowerGuard >a : RoyalGuard >isFollower : () => this is FollowerGuard @@ -78,7 +78,7 @@ let b: GuardInterface; >GuardInterface : GuardInterface if (b.isLeader()) { ->b.isLeader() : this is LeadGuard +>b.isLeader() : boolean >b.isLeader : () => this is LeadGuard >b : GuardInterface >isLeader : () => this is LeadGuard @@ -90,7 +90,7 @@ if (b.isLeader()) { >lead : () => void } else if (b.isFollower()) { ->b.isFollower() : this is FollowerGuard +>b.isFollower() : boolean >b.isFollower : () => this is FollowerGuard >b : GuardInterface >isFollower : () => this is FollowerGuard @@ -103,8 +103,8 @@ else if (b.isFollower()) { } if (((a.isLeader)())) { ->((a.isLeader)()) : this is LeadGuard ->(a.isLeader)() : this is LeadGuard +>((a.isLeader)()) : boolean +>(a.isLeader)() : boolean >(a.isLeader) : () => this is LeadGuard >a.isLeader : () => this is LeadGuard >a : RoyalGuard @@ -117,8 +117,8 @@ if (((a.isLeader)())) { >lead : () => void } else if (((a).isFollower())) { ->((a).isFollower()) : this is FollowerGuard ->(a).isFollower() : this is FollowerGuard +>((a).isFollower()) : boolean +>(a).isFollower() : boolean >(a).isFollower : () => this is FollowerGuard >(a) : RoyalGuard >a : RoyalGuard @@ -132,8 +132,8 @@ else if (((a).isFollower())) { } if (((a["isLeader"])())) { ->((a["isLeader"])()) : this is LeadGuard ->(a["isLeader"])() : this is LeadGuard +>((a["isLeader"])()) : boolean +>(a["isLeader"])() : boolean >(a["isLeader"]) : () => this is LeadGuard >a["isLeader"] : () => this is LeadGuard >a : RoyalGuard @@ -146,8 +146,8 @@ if (((a["isLeader"])())) { >lead : () => void } else if (((a)["isFollower"]())) { ->((a)["isFollower"]()) : this is FollowerGuard ->(a)["isFollower"]() : this is FollowerGuard +>((a)["isFollower"]()) : boolean +>(a)["isFollower"]() : boolean >(a)["isFollower"] : () => this is FollowerGuard >(a) : RoyalGuard >a : RoyalGuard @@ -166,7 +166,7 @@ var holder2 = {a}; >a : RoyalGuard if (holder2.a.isLeader()) { ->holder2.a.isLeader() : this is LeadGuard +>holder2.a.isLeader() : boolean >holder2.a.isLeader : () => this is LeadGuard >holder2.a : RoyalGuard >holder2 : { a: RoyalGuard; } @@ -232,7 +232,7 @@ let guard = new ArrowGuard(); >ArrowGuard : typeof ArrowGuard if (guard.isElite()) { ->guard.isElite() : this is ArrowElite +>guard.isElite() : boolean >guard.isElite : () => this is ArrowElite >guard : ArrowGuard >isElite : () => this is ArrowElite @@ -244,7 +244,7 @@ if (guard.isElite()) { >defend : () => void } else if (guard.isMedic()) { ->guard.isMedic() : this is ArrowMedic +>guard.isMedic() : boolean >guard.isMedic : () => this is ArrowMedic >guard : ArrowGuard >isMedic : () => this is ArrowMedic @@ -297,7 +297,7 @@ let crate: Crate<{}>; >Crate : Crate if (crate.isSundries()) { ->crate.isSundries() : this is Crate +>crate.isSundries() : boolean >crate.isSundries : () => this is Crate >crate : Crate<{}> >isSundries : () => this is Crate @@ -312,7 +312,7 @@ if (crate.isSundries()) { >true : boolean } else if (crate.isSupplies()) { ->crate.isSupplies() : this is Crate +>crate.isSupplies() : boolean >crate.isSupplies : () => this is Crate >crate : Crate<{}> >isSupplies : () => this is Crate @@ -405,7 +405,7 @@ a.isFollower = mimic.isFollower; >isFollower : () => this is MimicFollower if (mimic.isFollower()) { ->mimic.isFollower() : this is MimicFollower +>mimic.isFollower() : boolean >mimic.isFollower : () => this is MimicFollower >mimic : MimicGuard >isFollower : () => this is MimicFollower diff --git a/tests/baselines/reference/typeGuardOfFormFunctionEquality.symbols b/tests/baselines/reference/typeGuardOfFormFunctionEquality.symbols new file mode 100644 index 00000000000..f1384050176 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormFunctionEquality.symbols @@ -0,0 +1,41 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts === +declare function isString1(a: number, b: Object): b is string; +>isString1 : Symbol(isString1, Decl(typeGuardOfFormFunctionEquality.ts, 0, 0)) +>a : Symbol(a, Decl(typeGuardOfFormFunctionEquality.ts, 0, 27)) +>b : Symbol(b, Decl(typeGuardOfFormFunctionEquality.ts, 0, 37)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>b : Symbol(b, Decl(typeGuardOfFormFunctionEquality.ts, 0, 37)) + +declare function isString2(a: Object): a is string; +>isString2 : Symbol(isString2, Decl(typeGuardOfFormFunctionEquality.ts, 0, 62)) +>a : Symbol(a, Decl(typeGuardOfFormFunctionEquality.ts, 2, 27)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>a : Symbol(a, Decl(typeGuardOfFormFunctionEquality.ts, 2, 27)) + +switch (isString1(0, "")) { +>isString1 : Symbol(isString1, Decl(typeGuardOfFormFunctionEquality.ts, 0, 0)) + + case isString2(""): +>isString2 : Symbol(isString2, Decl(typeGuardOfFormFunctionEquality.ts, 0, 62)) + + default: +} + +var x = isString1(0, "") === isString2(""); +>x : Symbol(x, Decl(typeGuardOfFormFunctionEquality.ts, 9, 3)) +>isString1 : Symbol(isString1, Decl(typeGuardOfFormFunctionEquality.ts, 0, 0)) +>isString2 : Symbol(isString2, Decl(typeGuardOfFormFunctionEquality.ts, 0, 62)) + +function isString3(a: number, b: number, c: Object): c is string { +>isString3 : Symbol(isString3, Decl(typeGuardOfFormFunctionEquality.ts, 9, 43)) +>a : Symbol(a, Decl(typeGuardOfFormFunctionEquality.ts, 11, 19)) +>b : Symbol(b, Decl(typeGuardOfFormFunctionEquality.ts, 11, 29)) +>c : Symbol(c, Decl(typeGuardOfFormFunctionEquality.ts, 11, 40)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>c : Symbol(c, Decl(typeGuardOfFormFunctionEquality.ts, 11, 40)) + + return isString1(0, c); +>isString1 : Symbol(isString1, Decl(typeGuardOfFormFunctionEquality.ts, 0, 0)) +>c : Symbol(c, Decl(typeGuardOfFormFunctionEquality.ts, 11, 40)) +} + diff --git a/tests/baselines/reference/typeGuardOfFormFunctionEquality.types b/tests/baselines/reference/typeGuardOfFormFunctionEquality.types new file mode 100644 index 00000000000..55f2e2aac0e --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormFunctionEquality.types @@ -0,0 +1,54 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormFunctionEquality.ts === +declare function isString1(a: number, b: Object): b is string; +>isString1 : (a: number, b: Object) => b is string +>a : number +>b : Object +>Object : Object +>b : any + +declare function isString2(a: Object): a is string; +>isString2 : (a: Object) => a is string +>a : Object +>Object : Object +>a : any + +switch (isString1(0, "")) { +>isString1(0, "") : boolean +>isString1 : (a: number, b: Object) => b is string +>0 : number +>"" : string + + case isString2(""): +>isString2("") : boolean +>isString2 : (a: Object) => a is string +>"" : string + + default: +} + +var x = isString1(0, "") === isString2(""); +>x : boolean +>isString1(0, "") === isString2("") : boolean +>isString1(0, "") : boolean +>isString1 : (a: number, b: Object) => b is string +>0 : number +>"" : string +>isString2("") : boolean +>isString2 : (a: Object) => a is string +>"" : string + +function isString3(a: number, b: number, c: Object): c is string { +>isString3 : (a: number, b: number, c: Object) => c is string +>a : number +>b : number +>c : Object +>Object : Object +>c : any + + return isString1(0, c); +>isString1(0, c) : boolean +>isString1 : (a: number, b: Object) => b is string +>0 : number +>c : Object +} + diff --git a/tests/baselines/reference/typeGuardOfFormIsType.types b/tests/baselines/reference/typeGuardOfFormIsType.types index aa8f8cc7d60..e2059be7b63 100644 --- a/tests/baselines/reference/typeGuardOfFormIsType.types +++ b/tests/baselines/reference/typeGuardOfFormIsType.types @@ -67,7 +67,7 @@ str = isC1(c1Orc2) && c1Orc2.p1; // C1 >str = isC1(c1Orc2) && c1Orc2.p1 : string >str : string >isC1(c1Orc2) && c1Orc2.p1 : string ->isC1(c1Orc2) : x is C1 +>isC1(c1Orc2) : boolean >isC1 : (x: any) => x is C1 >c1Orc2 : C1 | C2 >c1Orc2.p1 : string @@ -78,7 +78,7 @@ num = isC2(c1Orc2) && c1Orc2.p2; // C2 >num = isC2(c1Orc2) && c1Orc2.p2 : number >num : number >isC2(c1Orc2) && c1Orc2.p2 : number ->isC2(c1Orc2) : x is C2 +>isC2(c1Orc2) : boolean >isC2 : (x: any) => x is C2 >c1Orc2 : C1 | C2 >c1Orc2.p2 : number @@ -89,7 +89,7 @@ str = isD1(c1Orc2) && c1Orc2.p1; // D1 >str = isD1(c1Orc2) && c1Orc2.p1 : string >str : string >isD1(c1Orc2) && c1Orc2.p1 : string ->isD1(c1Orc2) : x is D1 +>isD1(c1Orc2) : boolean >isD1 : (x: any) => x is D1 >c1Orc2 : C1 | C2 >c1Orc2.p1 : string @@ -100,7 +100,7 @@ num = isD1(c1Orc2) && c1Orc2.p3; // D1 >num = isD1(c1Orc2) && c1Orc2.p3 : number >num : number >isD1(c1Orc2) && c1Orc2.p3 : number ->isD1(c1Orc2) : x is D1 +>isD1(c1Orc2) : boolean >isD1 : (x: any) => x is D1 >c1Orc2 : C1 | C2 >c1Orc2.p3 : number @@ -116,7 +116,7 @@ num = isC2(c2Ord1) && c2Ord1.p2; // C2 >num = isC2(c2Ord1) && c2Ord1.p2 : number >num : number >isC2(c2Ord1) && c2Ord1.p2 : number ->isC2(c2Ord1) : x is C2 +>isC2(c2Ord1) : boolean >isC2 : (x: any) => x is C2 >c2Ord1 : C2 | D1 >c2Ord1.p2 : number @@ -127,7 +127,7 @@ num = isD1(c2Ord1) && c2Ord1.p3; // D1 >num = isD1(c2Ord1) && c2Ord1.p3 : number >num : number >isD1(c2Ord1) && c2Ord1.p3 : number ->isD1(c2Ord1) : x is D1 +>isD1(c2Ord1) : boolean >isD1 : (x: any) => x is D1 >c2Ord1 : C2 | D1 >c2Ord1.p3 : number @@ -138,7 +138,7 @@ str = isD1(c2Ord1) && c2Ord1.p1; // D1 >str = isD1(c2Ord1) && c2Ord1.p1 : string >str : string >isD1(c2Ord1) && c2Ord1.p1 : string ->isD1(c2Ord1) : x is D1 +>isD1(c2Ord1) : boolean >isD1 : (x: any) => x is D1 >c2Ord1 : C2 | D1 >c2Ord1.p1 : string @@ -150,7 +150,7 @@ var r2: C2 | D1 = isC1(c2Ord1) && c2Ord1; // C2 | D1 >C2 : C2 >D1 : D1 >isC1(c2Ord1) && c2Ord1 : D1 ->isC1(c2Ord1) : x is C1 +>isC1(c2Ord1) : boolean >isC1 : (x: any) => x is C1 >c2Ord1 : C2 | D1 >c2Ord1 : D1 diff --git a/tests/baselines/reference/typeGuardOfFormIsTypeOnInterfaces.types b/tests/baselines/reference/typeGuardOfFormIsTypeOnInterfaces.types index 4e28e6a4d38..ea169e95413 100644 --- a/tests/baselines/reference/typeGuardOfFormIsTypeOnInterfaces.types +++ b/tests/baselines/reference/typeGuardOfFormIsTypeOnInterfaces.types @@ -98,7 +98,7 @@ str = isC1(c1Orc2) && c1Orc2.p1; // C1 >str = isC1(c1Orc2) && c1Orc2.p1 : string >str : string >isC1(c1Orc2) && c1Orc2.p1 : string ->isC1(c1Orc2) : x is C1 +>isC1(c1Orc2) : boolean >isC1 : (x: any) => x is C1 >c1Orc2 : C1 | C2 >c1Orc2.p1 : string @@ -109,7 +109,7 @@ num = isC2(c1Orc2) && c1Orc2.p2; // C2 >num = isC2(c1Orc2) && c1Orc2.p2 : number >num : number >isC2(c1Orc2) && c1Orc2.p2 : number ->isC2(c1Orc2) : x is C2 +>isC2(c1Orc2) : boolean >isC2 : (x: any) => x is C2 >c1Orc2 : C1 | C2 >c1Orc2.p2 : number @@ -120,7 +120,7 @@ str = isD1(c1Orc2) && c1Orc2.p1; // D1 >str = isD1(c1Orc2) && c1Orc2.p1 : string >str : string >isD1(c1Orc2) && c1Orc2.p1 : string ->isD1(c1Orc2) : x is D1 +>isD1(c1Orc2) : boolean >isD1 : (x: any) => x is D1 >c1Orc2 : C1 | C2 >c1Orc2.p1 : string @@ -131,7 +131,7 @@ num = isD1(c1Orc2) && c1Orc2.p3; // D1 >num = isD1(c1Orc2) && c1Orc2.p3 : number >num : number >isD1(c1Orc2) && c1Orc2.p3 : number ->isD1(c1Orc2) : x is D1 +>isD1(c1Orc2) : boolean >isD1 : (x: any) => x is D1 >c1Orc2 : C1 | C2 >c1Orc2.p3 : number @@ -147,7 +147,7 @@ num = isC2(c2Ord1) && c2Ord1.p2; // C2 >num = isC2(c2Ord1) && c2Ord1.p2 : number >num : number >isC2(c2Ord1) && c2Ord1.p2 : number ->isC2(c2Ord1) : x is C2 +>isC2(c2Ord1) : boolean >isC2 : (x: any) => x is C2 >c2Ord1 : C2 | D1 >c2Ord1.p2 : number @@ -158,7 +158,7 @@ num = isD1(c2Ord1) && c2Ord1.p3; // D1 >num = isD1(c2Ord1) && c2Ord1.p3 : number >num : number >isD1(c2Ord1) && c2Ord1.p3 : number ->isD1(c2Ord1) : x is D1 +>isD1(c2Ord1) : boolean >isD1 : (x: any) => x is D1 >c2Ord1 : C2 | D1 >c2Ord1.p3 : number @@ -169,7 +169,7 @@ str = isD1(c2Ord1) && c2Ord1.p1; // D1 >str = isD1(c2Ord1) && c2Ord1.p1 : string >str : string >isD1(c2Ord1) && c2Ord1.p1 : string ->isD1(c2Ord1) : x is D1 +>isD1(c2Ord1) : boolean >isD1 : (x: any) => x is D1 >c2Ord1 : C2 | D1 >c2Ord1.p1 : string @@ -181,7 +181,7 @@ var r2: C2 | D1 = isC1(c2Ord1) && c2Ord1; // C2 | D1 >C2 : C2 >D1 : D1 >isC1(c2Ord1) && c2Ord1 : D1 ->isC1(c2Ord1) : x is C1 +>isC1(c2Ord1) : boolean >isC1 : (x: any) => x is C1 >c2Ord1 : C2 | D1 >c2Ord1 : D1 diff --git a/tests/baselines/reference/typeGuardOfFormThisMember.errors.txt b/tests/baselines/reference/typeGuardOfFormThisMember.errors.txt new file mode 100644 index 00000000000..21dc896fbd9 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormThisMember.errors.txt @@ -0,0 +1,130 @@ +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(4,10): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(5,17): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(11,22): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(14,16): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(34,8): error TS2339: Property 'content' does not exist on type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(36,9): error TS2339: Property 'host' does not exist on type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(37,9): error TS2339: Property 'content' does not exist on type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(41,8): error TS2339: Property 'children' does not exist on type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(44,8): error TS2339: Property 'host' does not exist on type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(57,13): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(58,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(63,9): error TS2339: Property 'lead' does not exist on type 'GenericGuard'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(66,9): error TS2339: Property 'follow' does not exist on type 'GenericGuard'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(70,19): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts(79,11): error TS2339: Property 'do' does not exist on type 'SpecificGuard'. + + +==== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts (15 errors) ==== + // There's a 'File' class in the stdlib, wrap with a namespace to avoid collision + namespace Test { + export class FileSystemObject { + isFSO: this is FileSystemObject; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + get isFile(): this is File { + ~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + return this instanceof File; + } + set isFile(param) { + // noop + } + get isDirectory(): this is Directory { + ~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + return this instanceof Directory; + } + isNetworked: this is (Networked & this); + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + constructor(public path: string) {} + } + + export class File extends FileSystemObject { + constructor(path: string, public content: string) { super(path); } + } + export class Directory extends FileSystemObject { + children: FileSystemObject[]; + } + export interface Networked { + host: string; + } + + let file: FileSystemObject = new File("foo/bar.txt", "foo"); + file.isNetworked = false; + file.isFSO = file.isFile; + file.isFile = true; + let x = file.isFile; + if (file.isFile) { + file.content; + ~~~~~~~ +!!! error TS2339: Property 'content' does not exist on type 'FileSystemObject'. + if (file.isNetworked) { + file.host; + ~~~~ +!!! error TS2339: Property 'host' does not exist on type 'FileSystemObject'. + file.content; + ~~~~~~~ +!!! error TS2339: Property 'content' does not exist on type 'FileSystemObject'. + } + } + else if (file.isDirectory) { + file.children; + ~~~~~~~~ +!!! error TS2339: Property 'children' does not exist on type 'FileSystemObject'. + } + else if (file.isNetworked) { + file.host; + ~~~~ +!!! error TS2339: Property 'host' does not exist on type 'FileSystemObject'. + } + + interface GenericLeadGuard extends GenericGuard { + lead(): void; + } + + interface GenericFollowerGuard extends GenericGuard { + follow(): void; + } + + interface GenericGuard { + target: T; + isLeader: this is (GenericLeadGuard); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + isFollower: this is GenericFollowerGuard; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + } + + let guard: GenericGuard; + if (guard.isLeader) { + guard.lead(); + ~~~~ +!!! error TS2339: Property 'lead' does not exist on type 'GenericGuard'. + } + else if (guard.isFollower) { + guard.follow(); + ~~~~~~ +!!! error TS2339: Property 'follow' does not exist on type 'GenericGuard'. + } + + interface SpecificGuard { + isMoreSpecific: this is MoreSpecificGuard; + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + } + + interface MoreSpecificGuard extends SpecificGuard { + do(): void; + } + + let general: SpecificGuard; + if (general.isMoreSpecific) { + general.do(); + ~~ +!!! error TS2339: Property 'do' does not exist on type 'SpecificGuard'. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardOfFormThisMember.symbols b/tests/baselines/reference/typeGuardOfFormThisMember.symbols deleted file mode 100644 index 50e15b68ca4..00000000000 --- a/tests/baselines/reference/typeGuardOfFormThisMember.symbols +++ /dev/null @@ -1,240 +0,0 @@ -=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts === -// There's a 'File' class in the stdlib, wrap with a namespace to avoid collision -namespace Test { ->Test : Symbol(Test, Decl(typeGuardOfFormThisMember.ts, 0, 0)) - - export class FileSystemObject { ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) - - isFSO: this is FileSystemObject; ->isFSO : Symbol(isFSO, Decl(typeGuardOfFormThisMember.ts, 2, 32)) ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) - - get isFile(): this is File { ->isFile : Symbol(isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->File : Symbol(File, Decl(typeGuardOfFormThisMember.ts, 15, 2)) - - return this instanceof File; ->this : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) ->File : Symbol(File, Decl(typeGuardOfFormThisMember.ts, 15, 2)) - } - set isFile(param) { ->isFile : Symbol(isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->param : Symbol(param, Decl(typeGuardOfFormThisMember.ts, 7, 13)) - - // noop - } - get isDirectory(): this is Directory { ->isDirectory : Symbol(isDirectory, Decl(typeGuardOfFormThisMember.ts, 9, 3)) ->Directory : Symbol(Directory, Decl(typeGuardOfFormThisMember.ts, 19, 2)) - - return this instanceof Directory; ->this : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) ->Directory : Symbol(Directory, Decl(typeGuardOfFormThisMember.ts, 19, 2)) - } - isNetworked: this is (Networked & this); ->isNetworked : Symbol(isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) ->Networked : Symbol(Networked, Decl(typeGuardOfFormThisMember.ts, 22, 2)) - - constructor(public path: string) {} ->path : Symbol(path, Decl(typeGuardOfFormThisMember.ts, 14, 14)) - } - - export class File extends FileSystemObject { ->File : Symbol(File, Decl(typeGuardOfFormThisMember.ts, 15, 2)) ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) - - constructor(path: string, public content: string) { super(path); } ->path : Symbol(path, Decl(typeGuardOfFormThisMember.ts, 18, 14)) ->content : Symbol(content, Decl(typeGuardOfFormThisMember.ts, 18, 27)) ->super : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) ->path : Symbol(path, Decl(typeGuardOfFormThisMember.ts, 18, 14)) - } - export class Directory extends FileSystemObject { ->Directory : Symbol(Directory, Decl(typeGuardOfFormThisMember.ts, 19, 2)) ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) - - children: FileSystemObject[]; ->children : Symbol(children, Decl(typeGuardOfFormThisMember.ts, 20, 50)) ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) - } - export interface Networked { ->Networked : Symbol(Networked, Decl(typeGuardOfFormThisMember.ts, 22, 2)) - - host: string; ->host : Symbol(host, Decl(typeGuardOfFormThisMember.ts, 23, 29)) - } - - let file: FileSystemObject = new File("foo/bar.txt", "foo"); ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->FileSystemObject : Symbol(FileSystemObject, Decl(typeGuardOfFormThisMember.ts, 1, 16)) ->File : Symbol(File, Decl(typeGuardOfFormThisMember.ts, 15, 2)) - - file.isNetworked = false; ->file.isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) - - file.isFSO = file.isFile; ->file.isFSO : Symbol(FileSystemObject.isFSO, Decl(typeGuardOfFormThisMember.ts, 2, 32)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isFSO : Symbol(FileSystemObject.isFSO, Decl(typeGuardOfFormThisMember.ts, 2, 32)) ->file.isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) - - file.isFile = true; ->file.isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) - - let x = file.isFile; ->x : Symbol(x, Decl(typeGuardOfFormThisMember.ts, 31, 4)) ->file.isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) - - if (file.isFile) { ->file.isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isFile : Symbol(FileSystemObject.isFile, Decl(typeGuardOfFormThisMember.ts, 3, 34), Decl(typeGuardOfFormThisMember.ts, 6, 3)) - - file.content; ->file.content : Symbol(File.content, Decl(typeGuardOfFormThisMember.ts, 18, 27)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->content : Symbol(File.content, Decl(typeGuardOfFormThisMember.ts, 18, 27)) - - if (file.isNetworked) { ->file.isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) - - file.host; ->file.host : Symbol(Networked.host, Decl(typeGuardOfFormThisMember.ts, 23, 29)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->host : Symbol(Networked.host, Decl(typeGuardOfFormThisMember.ts, 23, 29)) - - file.content; ->file.content : Symbol(File.content, Decl(typeGuardOfFormThisMember.ts, 18, 27)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->content : Symbol(File.content, Decl(typeGuardOfFormThisMember.ts, 18, 27)) - } - } - else if (file.isDirectory) { ->file.isDirectory : Symbol(FileSystemObject.isDirectory, Decl(typeGuardOfFormThisMember.ts, 9, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isDirectory : Symbol(FileSystemObject.isDirectory, Decl(typeGuardOfFormThisMember.ts, 9, 3)) - - file.children; ->file.children : Symbol(Directory.children, Decl(typeGuardOfFormThisMember.ts, 20, 50)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->children : Symbol(Directory.children, Decl(typeGuardOfFormThisMember.ts, 20, 50)) - } - else if (file.isNetworked) { ->file.isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->isNetworked : Symbol(FileSystemObject.isNetworked, Decl(typeGuardOfFormThisMember.ts, 12, 3)) - - file.host; ->file.host : Symbol(Networked.host, Decl(typeGuardOfFormThisMember.ts, 23, 29)) ->file : Symbol(file, Decl(typeGuardOfFormThisMember.ts, 27, 4)) ->host : Symbol(Networked.host, Decl(typeGuardOfFormThisMember.ts, 23, 29)) - } - - interface GenericLeadGuard extends GenericGuard { ->GenericLeadGuard : Symbol(GenericLeadGuard, Decl(typeGuardOfFormThisMember.ts, 44, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 46, 28)) ->GenericGuard : Symbol(GenericGuard, Decl(typeGuardOfFormThisMember.ts, 52, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 46, 28)) - - lead(): void; ->lead : Symbol(lead, Decl(typeGuardOfFormThisMember.ts, 46, 56)) - } - - interface GenericFollowerGuard extends GenericGuard { ->GenericFollowerGuard : Symbol(GenericFollowerGuard, Decl(typeGuardOfFormThisMember.ts, 48, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 50, 32)) ->GenericGuard : Symbol(GenericGuard, Decl(typeGuardOfFormThisMember.ts, 52, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 50, 32)) - - follow(): void; ->follow : Symbol(follow, Decl(typeGuardOfFormThisMember.ts, 50, 60)) - } - - interface GenericGuard { ->GenericGuard : Symbol(GenericGuard, Decl(typeGuardOfFormThisMember.ts, 52, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 54, 24)) - - target: T; ->target : Symbol(target, Decl(typeGuardOfFormThisMember.ts, 54, 28)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 54, 24)) - - isLeader: this is (GenericLeadGuard); ->isLeader : Symbol(isLeader, Decl(typeGuardOfFormThisMember.ts, 55, 12)) ->GenericLeadGuard : Symbol(GenericLeadGuard, Decl(typeGuardOfFormThisMember.ts, 44, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 54, 24)) - - isFollower: this is GenericFollowerGuard; ->isFollower : Symbol(isFollower, Decl(typeGuardOfFormThisMember.ts, 56, 42)) ->GenericFollowerGuard : Symbol(GenericFollowerGuard, Decl(typeGuardOfFormThisMember.ts, 48, 2)) ->T : Symbol(T, Decl(typeGuardOfFormThisMember.ts, 54, 24)) - } - - let guard: GenericGuard; ->guard : Symbol(guard, Decl(typeGuardOfFormThisMember.ts, 60, 4)) ->GenericGuard : Symbol(GenericGuard, Decl(typeGuardOfFormThisMember.ts, 52, 2)) ->File : Symbol(File, Decl(typeGuardOfFormThisMember.ts, 15, 2)) - - if (guard.isLeader) { ->guard.isLeader : Symbol(GenericGuard.isLeader, Decl(typeGuardOfFormThisMember.ts, 55, 12)) ->guard : Symbol(guard, Decl(typeGuardOfFormThisMember.ts, 60, 4)) ->isLeader : Symbol(GenericGuard.isLeader, Decl(typeGuardOfFormThisMember.ts, 55, 12)) - - guard.lead(); ->guard.lead : Symbol(GenericLeadGuard.lead, Decl(typeGuardOfFormThisMember.ts, 46, 56)) ->guard : Symbol(guard, Decl(typeGuardOfFormThisMember.ts, 60, 4)) ->lead : Symbol(GenericLeadGuard.lead, Decl(typeGuardOfFormThisMember.ts, 46, 56)) - } - else if (guard.isFollower) { ->guard.isFollower : Symbol(GenericGuard.isFollower, Decl(typeGuardOfFormThisMember.ts, 56, 42)) ->guard : Symbol(guard, Decl(typeGuardOfFormThisMember.ts, 60, 4)) ->isFollower : Symbol(GenericGuard.isFollower, Decl(typeGuardOfFormThisMember.ts, 56, 42)) - - guard.follow(); ->guard.follow : Symbol(GenericFollowerGuard.follow, Decl(typeGuardOfFormThisMember.ts, 50, 60)) ->guard : Symbol(guard, Decl(typeGuardOfFormThisMember.ts, 60, 4)) ->follow : Symbol(GenericFollowerGuard.follow, Decl(typeGuardOfFormThisMember.ts, 50, 60)) - } - - interface SpecificGuard { ->SpecificGuard : Symbol(SpecificGuard, Decl(typeGuardOfFormThisMember.ts, 66, 2)) - - isMoreSpecific: this is MoreSpecificGuard; ->isMoreSpecific : Symbol(isMoreSpecific, Decl(typeGuardOfFormThisMember.ts, 68, 26)) ->MoreSpecificGuard : Symbol(MoreSpecificGuard, Decl(typeGuardOfFormThisMember.ts, 70, 2)) - } - - interface MoreSpecificGuard extends SpecificGuard { ->MoreSpecificGuard : Symbol(MoreSpecificGuard, Decl(typeGuardOfFormThisMember.ts, 70, 2)) ->SpecificGuard : Symbol(SpecificGuard, Decl(typeGuardOfFormThisMember.ts, 66, 2)) - - do(): void; ->do : Symbol(do, Decl(typeGuardOfFormThisMember.ts, 72, 52)) - } - - let general: SpecificGuard; ->general : Symbol(general, Decl(typeGuardOfFormThisMember.ts, 76, 4)) ->SpecificGuard : Symbol(SpecificGuard, Decl(typeGuardOfFormThisMember.ts, 66, 2)) - - if (general.isMoreSpecific) { ->general.isMoreSpecific : Symbol(SpecificGuard.isMoreSpecific, Decl(typeGuardOfFormThisMember.ts, 68, 26)) ->general : Symbol(general, Decl(typeGuardOfFormThisMember.ts, 76, 4)) ->isMoreSpecific : Symbol(SpecificGuard.isMoreSpecific, Decl(typeGuardOfFormThisMember.ts, 68, 26)) - - general.do(); ->general.do : Symbol(MoreSpecificGuard.do, Decl(typeGuardOfFormThisMember.ts, 72, 52)) ->general : Symbol(general, Decl(typeGuardOfFormThisMember.ts, 76, 4)) ->do : Symbol(MoreSpecificGuard.do, Decl(typeGuardOfFormThisMember.ts, 72, 52)) - } -} - diff --git a/tests/baselines/reference/typeGuardOfFormThisMember.types b/tests/baselines/reference/typeGuardOfFormThisMember.types deleted file mode 100644 index 68343947fb4..00000000000 --- a/tests/baselines/reference/typeGuardOfFormThisMember.types +++ /dev/null @@ -1,254 +0,0 @@ -=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMember.ts === -// There's a 'File' class in the stdlib, wrap with a namespace to avoid collision -namespace Test { ->Test : typeof Test - - export class FileSystemObject { ->FileSystemObject : FileSystemObject - - isFSO: this is FileSystemObject; ->isFSO : this is FileSystemObject ->FileSystemObject : FileSystemObject - - get isFile(): this is File { ->isFile : this is File ->File : File - - return this instanceof File; ->this instanceof File : boolean ->this : this ->File : typeof File - } - set isFile(param) { ->isFile : this is File ->param : boolean - - // noop - } - get isDirectory(): this is Directory { ->isDirectory : this is Directory ->Directory : Directory - - return this instanceof Directory; ->this instanceof Directory : boolean ->this : this ->Directory : typeof Directory - } - isNetworked: this is (Networked & this); ->isNetworked : this is Networked & this ->Networked : Networked - - constructor(public path: string) {} ->path : string - } - - export class File extends FileSystemObject { ->File : File ->FileSystemObject : FileSystemObject - - constructor(path: string, public content: string) { super(path); } ->path : string ->content : string ->super(path) : void ->super : typeof FileSystemObject ->path : string - } - export class Directory extends FileSystemObject { ->Directory : Directory ->FileSystemObject : FileSystemObject - - children: FileSystemObject[]; ->children : FileSystemObject[] ->FileSystemObject : FileSystemObject - } - export interface Networked { ->Networked : Networked - - host: string; ->host : string - } - - let file: FileSystemObject = new File("foo/bar.txt", "foo"); ->file : FileSystemObject ->FileSystemObject : FileSystemObject ->new File("foo/bar.txt", "foo") : File ->File : typeof File ->"foo/bar.txt" : string ->"foo" : string - - file.isNetworked = false; ->file.isNetworked = false : boolean ->file.isNetworked : this is Networked & FileSystemObject ->file : FileSystemObject ->isNetworked : this is Networked & FileSystemObject ->false : boolean - - file.isFSO = file.isFile; ->file.isFSO = file.isFile : this is File ->file.isFSO : this is FileSystemObject ->file : FileSystemObject ->isFSO : this is FileSystemObject ->file.isFile : this is File ->file : FileSystemObject ->isFile : this is File - - file.isFile = true; ->file.isFile = true : boolean ->file.isFile : this is File ->file : FileSystemObject ->isFile : this is File ->true : boolean - - let x = file.isFile; ->x : boolean ->file.isFile : this is File ->file : FileSystemObject ->isFile : this is File - - if (file.isFile) { ->file.isFile : this is File ->file : FileSystemObject ->isFile : this is File - - file.content; ->file.content : string ->file : File ->content : string - - if (file.isNetworked) { ->file.isNetworked : this is Networked & File ->file : File ->isNetworked : this is Networked & File - - file.host; ->file.host : string ->file : Networked & File ->host : string - - file.content; ->file.content : string ->file : Networked & File ->content : string - } - } - else if (file.isDirectory) { ->file.isDirectory : this is Directory ->file : FileSystemObject ->isDirectory : this is Directory - - file.children; ->file.children : FileSystemObject[] ->file : Directory ->children : FileSystemObject[] - } - else if (file.isNetworked) { ->file.isNetworked : this is Networked & FileSystemObject ->file : FileSystemObject ->isNetworked : this is Networked & FileSystemObject - - file.host; ->file.host : string ->file : Networked & FileSystemObject ->host : string - } - - interface GenericLeadGuard extends GenericGuard { ->GenericLeadGuard : GenericLeadGuard ->T : T ->GenericGuard : GenericGuard ->T : T - - lead(): void; ->lead : () => void - } - - interface GenericFollowerGuard extends GenericGuard { ->GenericFollowerGuard : GenericFollowerGuard ->T : T ->GenericGuard : GenericGuard ->T : T - - follow(): void; ->follow : () => void - } - - interface GenericGuard { ->GenericGuard : GenericGuard ->T : T - - target: T; ->target : T ->T : T - - isLeader: this is (GenericLeadGuard); ->isLeader : this is GenericLeadGuard ->GenericLeadGuard : GenericLeadGuard ->T : T - - isFollower: this is GenericFollowerGuard; ->isFollower : this is GenericFollowerGuard ->GenericFollowerGuard : GenericFollowerGuard ->T : T - } - - let guard: GenericGuard; ->guard : GenericGuard ->GenericGuard : GenericGuard ->File : File - - if (guard.isLeader) { ->guard.isLeader : this is GenericLeadGuard ->guard : GenericGuard ->isLeader : this is GenericLeadGuard - - guard.lead(); ->guard.lead() : void ->guard.lead : () => void ->guard : GenericLeadGuard ->lead : () => void - } - else if (guard.isFollower) { ->guard.isFollower : this is GenericFollowerGuard ->guard : GenericGuard ->isFollower : this is GenericFollowerGuard - - guard.follow(); ->guard.follow() : void ->guard.follow : () => void ->guard : GenericFollowerGuard ->follow : () => void - } - - interface SpecificGuard { ->SpecificGuard : SpecificGuard - - isMoreSpecific: this is MoreSpecificGuard; ->isMoreSpecific : this is MoreSpecificGuard ->MoreSpecificGuard : MoreSpecificGuard - } - - interface MoreSpecificGuard extends SpecificGuard { ->MoreSpecificGuard : MoreSpecificGuard ->SpecificGuard : SpecificGuard - - do(): void; ->do : () => void - } - - let general: SpecificGuard; ->general : SpecificGuard ->SpecificGuard : SpecificGuard - - if (general.isMoreSpecific) { ->general.isMoreSpecific : this is MoreSpecificGuard ->general : SpecificGuard ->isMoreSpecific : this is MoreSpecificGuard - - general.do(); ->general.do() : void ->general.do : () => void ->general : MoreSpecificGuard ->do : () => void - } -} - diff --git a/tests/baselines/reference/typeGuardOfFormThisMemberErrors.errors.txt b/tests/baselines/reference/typeGuardOfFormThisMemberErrors.errors.txt index 754cbffe6e6..88e9d41cc7b 100644 --- a/tests/baselines/reference/typeGuardOfFormThisMemberErrors.errors.txt +++ b/tests/baselines/reference/typeGuardOfFormThisMemberErrors.errors.txt @@ -1,27 +1,32 @@ -tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(29,2): error TS1226: Type predicate 'this is File' is not assignable to 'this is Networked & FileSystemObject'. - Type 'File' is not assignable to type 'Networked & FileSystemObject'. - Type 'File' is not assignable to type 'Networked'. - Property 'host' is missing in type 'File'. -tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(31,2): error TS1226: Type predicate 'this is FileSystemObject' is not assignable to 'this is File'. - Type 'FileSystemObject' is not assignable to type 'File'. - Property 'content' is missing in type 'FileSystemObject'. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(4,10): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(5,17): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(11,22): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts(14,16): error TS1228: A type predicate is only allowed in return type position for functions and methods. -==== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts (2 errors) ==== +==== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.ts (4 errors) ==== // There's a 'File' class in the stdlib, wrap with a namespace to avoid collision namespace Test { export class FileSystemObject { isFSO: this is FileSystemObject; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. get isFile(): this is File { + ~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. return this instanceof File; } set isFile(param) { // noop } get isDirectory(): this is Directory { + ~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. return this instanceof Directory; } isNetworked: this is (Networked & this); + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. constructor(public path: string) {} } @@ -37,15 +42,6 @@ tests/cases/conformance/expressions/typeGuards/typeGuardOfFormThisMemberErrors.t let file: FileSystemObject = new File("foo/bar.txt", "foo"); file.isNetworked = file.isFile; - ~~~~~~~~~~~~~~~~ -!!! error TS1226: Type predicate 'this is File' is not assignable to 'this is Networked & FileSystemObject'. -!!! error TS1226: Type 'File' is not assignable to type 'Networked & FileSystemObject'. -!!! error TS1226: Type 'File' is not assignable to type 'Networked'. -!!! error TS1226: Property 'host' is missing in type 'File'. file.isFSO = file.isNetworked; file.isFile = file.isFSO; - ~~~~~~~~~~~ -!!! error TS1226: Type predicate 'this is FileSystemObject' is not assignable to 'this is File'. -!!! error TS1226: Type 'FileSystemObject' is not assignable to type 'File'. -!!! error TS1226: Property 'content' is missing in type 'FileSystemObject'. } \ No newline at end of file diff --git a/tests/baselines/reference/typePredicateOnVariableDeclaration01.errors.txt b/tests/baselines/reference/typePredicateOnVariableDeclaration01.errors.txt new file mode 100644 index 00000000000..b713dd12ae7 --- /dev/null +++ b/tests/baselines/reference/typePredicateOnVariableDeclaration01.errors.txt @@ -0,0 +1,8 @@ +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts(2,8): error TS1228: A type predicate is only allowed in return type position for functions and methods. + + +==== tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration01.ts (1 errors) ==== + + var x: this is string; + ~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. \ No newline at end of file diff --git a/tests/baselines/reference/typePredicateOnVariableDeclaration02.errors.txt b/tests/baselines/reference/typePredicateOnVariableDeclaration02.errors.txt new file mode 100644 index 00000000000..af967b22b79 --- /dev/null +++ b/tests/baselines/reference/typePredicateOnVariableDeclaration02.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts(2,8): error TS2304: Cannot find name 'z'. +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts(2,8): error TS4025: Exported variable 'y' has or is using private name 'z'. +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts(2,10): error TS1005: '=' expected. +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts(2,10): error TS2304: Cannot find name 'is'. +tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts(2,13): error TS1005: ',' expected. + + +==== tests/cases/conformance/expressions/typeGuards/typePredicateOnVariableDeclaration02.ts (5 errors) ==== + + var y: z is number; + ~ +!!! error TS2304: Cannot find name 'z'. + ~ +!!! error TS4025: Exported variable 'y' has or is using private name 'z'. + ~~ +!!! error TS1005: '=' expected. + ~~ +!!! error TS2304: Cannot find name 'is'. + ~~~~~~ +!!! error TS1005: ',' expected. \ No newline at end of file diff --git a/tests/baselines/reference/unionAndIntersectionInference1.types b/tests/baselines/reference/unionAndIntersectionInference1.types index 073a677b659..5d23688f0b7 100644 --- a/tests/baselines/reference/unionAndIntersectionInference1.types +++ b/tests/baselines/reference/unionAndIntersectionInference1.types @@ -110,7 +110,7 @@ function foo1(value: void|a): void { >a : a if (isVoid(value)) { ->isVoid(value) : value is void +>isVoid(value) : boolean >isVoid : (value: void | a) => value is void >value : void | a @@ -130,7 +130,7 @@ function baz1(value: void|a): void { >a : a if (isNonVoid(value)) { ->isNonVoid(value) : value is a +>isNonVoid(value) : boolean >isNonVoid : (value: void | a) => value is a >value : void | a From 9960064bc30ba7556497246bb2bad426bee6f9c6 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 12 Feb 2016 16:30:19 -0800 Subject: [PATCH 35/55] classic resolution: don't perform folder walk if module name is relative --- src/compiler/program.ts | 27 ++++++++++++------- ...elativeNamesInClassicResolution.errors.txt | 11 ++++++++ .../relativeNamesInClassicResolution.ts | 7 +++++ tests/cases/unittests/moduleResolution.ts | 23 +++++++--------- 4 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/relativeNamesInClassicResolution.errors.txt create mode 100644 tests/cases/compiler/relativeNamesInClassicResolution.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 0acb13f02d3..56a08dcc7f2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -533,18 +533,25 @@ namespace ts { } let referencedSourceFile: string; - while (true) { - const searchName = normalizePath(combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); - if (referencedSourceFile) { - break; + if (moduleHasNonRelativeName(moduleName)) { + while (true) { + const searchName = normalizePath(combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + if (referencedSourceFile) { + break; + } + const parentPath = getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + break; + } + containingDirectory = parentPath; } - const parentPath = getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { - break; - } - containingDirectory = parentPath; } + else { + const candidate = normalizePath(combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + } + return referencedSourceFile ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations } diff --git a/tests/baselines/reference/relativeNamesInClassicResolution.errors.txt b/tests/baselines/reference/relativeNamesInClassicResolution.errors.txt new file mode 100644 index 00000000000..65a42775331 --- /dev/null +++ b/tests/baselines/reference/relativeNamesInClassicResolution.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/somefolder/a.ts(2,17): error TS2307: Cannot find module './b'. + + +==== tests/cases/compiler/somefolder/a.ts (1 errors) ==== + + import {x} from "./b" + ~~~~~ +!!! error TS2307: Cannot find module './b'. + +==== tests/cases/compiler/b.ts (0 errors) ==== + export let x = 1; \ No newline at end of file diff --git a/tests/cases/compiler/relativeNamesInClassicResolution.ts b/tests/cases/compiler/relativeNamesInClassicResolution.ts new file mode 100644 index 00000000000..7c73a2e3cad --- /dev/null +++ b/tests/cases/compiler/relativeNamesInClassicResolution.ts @@ -0,0 +1,7 @@ +// @module:amd + +// @filename: somefolder/a.ts +import {x} from "./b" + +// @filename: b.ts +export let x = 1; \ No newline at end of file diff --git a/tests/cases/unittests/moduleResolution.ts b/tests/cases/unittests/moduleResolution.ts index 1173f579f2c..9a52d9896e4 100644 --- a/tests/cases/unittests/moduleResolution.ts +++ b/tests/cases/unittests/moduleResolution.ts @@ -48,7 +48,7 @@ module ts { return hasProperty(directories, path); }, fileExists: path => { - assert.isTrue(hasProperty(directories, getDirectoryPath(path)), "'fileExists' request in non-existing directory"); + assert.isTrue(hasProperty(directories, getDirectoryPath(path)), `'fileExists' '${path}' request in non-existing directory`); return hasProperty(map, path); } } @@ -814,7 +814,6 @@ import b = require("./moduleB.ts"); it ("classic + rootDirs", () => { test(/*hasDirectoryExists*/ false); - test(/*hasDirectoryExists*/ true); function test(hasDirectoryExists: boolean) { let file1: File = { name: "/root/folder1/file1.ts" }; @@ -844,24 +843,20 @@ import b = require("./moduleB.ts"); "/root/generated/folder1/file1.d.ts", // then try alternative rootDir entry ]); - check("../folder1/file1_1", file3, file4, [ - // load from initial location + check("folder1/file1_1", file3, file4, [ + // current location + "/root/generated/folder2/folder1/file1_1.ts", + "/root/generated/folder2/folder1/file1_1.tsx", + "/root/generated/folder2/folder1/file1_1.d.ts", + // other entry in rootDirs "/root/generated/folder1/file1_1.ts", "/root/generated/folder1/file1_1.tsx", "/root/generated/folder1/file1_1.d.ts", - // load from alternative rootDir entry - "/root/folder1/file1_1.ts", - "/root/folder1/file1_1.tsx", - "/root/folder1/file1_1.d.ts", - // fallback to classic - // step1: initial location - "/root/generated/folder1/file1_1.ts", - "/root/generated/folder1/file1_1.tsx", - "/root/generated/folder1/file1_1.d.ts", - // step2: walk 1 level up + // fallback "/root/folder1/file1_1.ts", "/root/folder1/file1_1.tsx", "/root/folder1/file1_1.d.ts", + // found one ]); function check(name: string, container: File, expected: File, expectedFailedLookups: string[]) { From 8ef9599bac0a4559119de60907fcdfb8cded3465 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Sat, 13 Feb 2016 01:07:33 +0000 Subject: [PATCH 36/55] Allow extending and instantiating a private or protected class within itself --- src/compiler/checker.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2d1bd26442e..d6b67c456ca 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10192,12 +10192,11 @@ namespace ts { return true; } + const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); const declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); - const enclosingClassDeclaration = getContainingClass(node); - const enclosingClass = enclosingClassDeclaration ? getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassDeclaration)) : undefined; // A private or protected constructor can only be instantiated within it's own class - if (declaringClass !== enclosingClass) { + if (!isNodeWithinClass(node, declaringClassDeclaration)) { if (flags & NodeFlags.Private) { error(node, Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); } @@ -14076,11 +14075,14 @@ namespace ts { } function checkBaseTypeAccessibility(type: ObjectType, node: ExpressionWithTypeArguments) { - const signatures = getSignaturesOfType(type, SignatureKind.Construct); - if (signatures.length) { - const declaration = signatures[0].declaration; - if (declaration && declaration.flags & NodeFlags.Private) { - error(node, Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, (node.expression).text); + const typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); + if (!isNodeWithinClass(node, typeClassDeclaration)) { + const signatures = getSignaturesOfType(type, SignatureKind.Construct); + if (signatures.length) { + const declaration = signatures[0].declaration; + if (declaration && declaration.flags & NodeFlags.Private) { + error(node, Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, (node.expression).text); + } } } } @@ -15408,6 +15410,19 @@ namespace ts { return node.parent && node.parent.kind === SyntaxKind.ExpressionWithTypeArguments; } + function isNodeWithinClass(node: Node, classDeclaration: ClassLikeDeclaration) { + while (true) { + const containingClass = getContainingClass(node); + if (!containingClass) { + return false; + } + if (containingClass === classDeclaration) { + return true; + } + node = containingClass; + } + } + function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide: EntityName): ImportEqualsDeclaration | ExportAssignment { while (nodeOnRightSide.parent.kind === SyntaxKind.QualifiedName) { nodeOnRightSide = nodeOnRightSide.parent; From ec7e80e377fae0360bccb30f937adc2693c592ca Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Sat, 13 Feb 2016 01:10:47 +0000 Subject: [PATCH 37/55] Added tests and accept baselines --- .../classConstructorAccessibility4.errors.txt | 42 +++++++++++++++++++ .../classConstructorAccessibility4.ts | 33 +++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/baselines/reference/classConstructorAccessibility4.errors.txt create mode 100644 tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts diff --git a/tests/baselines/reference/classConstructorAccessibility4.errors.txt b/tests/baselines/reference/classConstructorAccessibility4.errors.txt new file mode 100644 index 00000000000..1cc50e3f4c8 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility4.errors.txt @@ -0,0 +1,42 @@ +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts(16,9): error TS2673: Constructor of class 'A' is private and only accessible within the class declaration. +tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts(33,9): error TS2674: Constructor of class 'D' is protected and only accessible within the class declaration. + + +==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts (2 errors) ==== + class A { + private constructor() { } + + method() { + class B { + method() { + new A(); // OK + } + } + + class C extends A { // OK + } + } + } + + var a = new A(); // error + ~~~~~~~ +!!! error TS2673: Constructor of class 'A' is private and only accessible within the class declaration. + + class D { + protected constructor() { } + + method() { + class E { + method() { + new D(); // OK + } + } + + class F extends D { // OK + } + } + } + + var d = new D(); // error + ~~~~~~~ +!!! error TS2674: Constructor of class 'D' is protected and only accessible within the class declaration. \ No newline at end of file diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts new file mode 100644 index 00000000000..e75f9be6fde --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts @@ -0,0 +1,33 @@ +class A { + private constructor() { } + + method() { + class B { + method() { + new A(); // OK + } + } + + class C extends A { // OK + } + } +} + +var a = new A(); // error + +class D { + protected constructor() { } + + method() { + class E { + method() { + new D(); // OK + } + } + + class F extends D { // OK + } + } +} + +var d = new D(); // error \ No newline at end of file From 3ecc42beb2b2fb95dde32b2b7d764e84c86540c0 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 12 Feb 2016 17:16:50 -0800 Subject: [PATCH 38/55] added misssing files --- .../relativeNamesInClassicResolution.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/baselines/reference/relativeNamesInClassicResolution.js diff --git a/tests/baselines/reference/relativeNamesInClassicResolution.js b/tests/baselines/reference/relativeNamesInClassicResolution.js new file mode 100644 index 00000000000..a6c5a06ea3d --- /dev/null +++ b/tests/baselines/reference/relativeNamesInClassicResolution.js @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/relativeNamesInClassicResolution.ts] //// + +//// [a.ts] + +import {x} from "./b" + +//// [b.ts] +export let x = 1; + +//// [a.js] +define(["require", "exports"], function (require, exports) { + "use strict"; +}); +//// [b.js] +define(["require", "exports"], function (require, exports) { + "use strict"; + exports.x = 1; +}); From c5d5d13e61f67f5cb8df0ee3417ee82f2b121bce Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 12 Feb 2016 17:28:10 -0800 Subject: [PATCH 39/55] Remove the 'module' option from 'tsconfig.json' files. --- src/compiler/tsconfig.json | 3 +-- src/server/tsconfig.json | 1 - src/services/tsconfig.json | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index ca297c087cf..5460f162935 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "module": "commonjs", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, @@ -8,9 +7,9 @@ "sourceMap": true }, "files": [ + "types.ts", "core.ts", "sys.ts", - "types.ts", "diagnosticInformationMap.generated.ts", "scanner.ts", "parser.ts", diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json index 2c8538c61e3..0772210cb15 100644 --- a/src/server/tsconfig.json +++ b/src/server/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "module": "commonjs", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 5dcee789243..001071ed88d 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "module": "commonjs", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, From e29be4b2d99f7fea5ff18e64011f518722a801c1 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 12 Feb 2016 17:39:46 -0800 Subject: [PATCH 40/55] Added the accidentally-ignored js files. --- .../declarationEmitIdentifierPredicates01.js | 16 +++++++ ...itIdentifierPredicatesWithPrivateName01.js | 16 +++++++ .../declarationEmitThisPredicates01.js | 43 +++++++++++++++++++ .../declarationEmitThisPredicates02.js | 34 +++++++++++++++ ...tionEmitThisPredicatesWithPrivateName01.js | 34 +++++++++++++++ ...tionEmitThisPredicatesWithPrivateName02.js | 23 ++++++++++ .../typeGuardOfFormFunctionEquality.js | 26 +++++++++++ .../typePredicateOnVariableDeclaration01.js | 10 +++++ .../typePredicateOnVariableDeclaration02.js | 6 +++ 9 files changed, 208 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitIdentifierPredicates01.js create mode 100644 tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.js create mode 100644 tests/baselines/reference/declarationEmitThisPredicates01.js create mode 100644 tests/baselines/reference/declarationEmitThisPredicates02.js create mode 100644 tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.js create mode 100644 tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.js create mode 100644 tests/baselines/reference/typeGuardOfFormFunctionEquality.js create mode 100644 tests/baselines/reference/typePredicateOnVariableDeclaration01.js create mode 100644 tests/baselines/reference/typePredicateOnVariableDeclaration02.js diff --git a/tests/baselines/reference/declarationEmitIdentifierPredicates01.js b/tests/baselines/reference/declarationEmitIdentifierPredicates01.js new file mode 100644 index 00000000000..73141eb9af9 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIdentifierPredicates01.js @@ -0,0 +1,16 @@ +//// [declarationEmitIdentifierPredicates01.ts] + +export function f(x: any): x is number { + return typeof x === "number"; +} + +//// [declarationEmitIdentifierPredicates01.js] +"use strict"; +function f(x) { + return typeof x === "number"; +} +exports.f = f; + + +//// [declarationEmitIdentifierPredicates01.d.ts] +export declare function f(x: any): x is number; diff --git a/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.js b/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.js new file mode 100644 index 00000000000..234542d8d99 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIdentifierPredicatesWithPrivateName01.js @@ -0,0 +1,16 @@ +//// [declarationEmitIdentifierPredicatesWithPrivateName01.ts] + +interface I { + a: number; +} + +export function f(x: any): x is I { + return typeof x.a === "number"; +} + +//// [declarationEmitIdentifierPredicatesWithPrivateName01.js] +"use strict"; +function f(x) { + return typeof x.a === "number"; +} +exports.f = f; diff --git a/tests/baselines/reference/declarationEmitThisPredicates01.js b/tests/baselines/reference/declarationEmitThisPredicates01.js new file mode 100644 index 00000000000..d05b5c46dad --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicates01.js @@ -0,0 +1,43 @@ +//// [declarationEmitThisPredicates01.ts] + +export class C { + m(): this is D { + return this instanceof D; + } +} + +export class D extends C { +} + +//// [declarationEmitThisPredicates01.js] +"use strict"; +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + function C() { + } + C.prototype.m = function () { + return this instanceof D; + }; + return C; +}()); +exports.C = C; +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.apply(this, arguments); + } + return D; +}(C)); +exports.D = D; + + +//// [declarationEmitThisPredicates01.d.ts] +export declare class C { + m(): this is D; +} +export declare class D extends C { +} diff --git a/tests/baselines/reference/declarationEmitThisPredicates02.js b/tests/baselines/reference/declarationEmitThisPredicates02.js new file mode 100644 index 00000000000..e938a615b6d --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicates02.js @@ -0,0 +1,34 @@ +//// [declarationEmitThisPredicates02.ts] + +export interface Foo { + a: string; + b: number; + c: boolean; +} + +export const obj = { + m(): this is Foo { + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } +} + +//// [declarationEmitThisPredicates02.js] +"use strict"; +exports.obj = { + m: function () { + var dis = this; + return dis.a != null && dis.b != null && dis.c != null; + } +}; + + +//// [declarationEmitThisPredicates02.d.ts] +export interface Foo { + a: string; + b: number; + c: boolean; +} +export declare const obj: { + m(): this is Foo; +}; diff --git a/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.js b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.js new file mode 100644 index 00000000000..e34dcc9810f --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName01.js @@ -0,0 +1,34 @@ +//// [declarationEmitThisPredicatesWithPrivateName01.ts] + +export class C { + m(): this is D { + return this instanceof D; + } +} + +class D extends C { +} + +//// [declarationEmitThisPredicatesWithPrivateName01.js] +"use strict"; +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + function C() { + } + C.prototype.m = function () { + return this instanceof D; + }; + return C; +}()); +exports.C = C; +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.apply(this, arguments); + } + return D; +}(C)); diff --git a/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.js b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.js new file mode 100644 index 00000000000..df0d5b7903a --- /dev/null +++ b/tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.js @@ -0,0 +1,23 @@ +//// [declarationEmitThisPredicatesWithPrivateName02.ts] + +interface Foo { + a: string; + b: number; + c: boolean; +} + +export const obj = { + m(): this is Foo { + let dis = this as Foo; + return dis.a != null && dis.b != null && dis.c != null; + } +} + +//// [declarationEmitThisPredicatesWithPrivateName02.js] +"use strict"; +exports.obj = { + m: function () { + var dis = this; + return dis.a != null && dis.b != null && dis.c != null; + } +}; diff --git a/tests/baselines/reference/typeGuardOfFormFunctionEquality.js b/tests/baselines/reference/typeGuardOfFormFunctionEquality.js new file mode 100644 index 00000000000..c15c1a739a9 --- /dev/null +++ b/tests/baselines/reference/typeGuardOfFormFunctionEquality.js @@ -0,0 +1,26 @@ +//// [typeGuardOfFormFunctionEquality.ts] +declare function isString1(a: number, b: Object): b is string; + +declare function isString2(a: Object): a is string; + +switch (isString1(0, "")) { + case isString2(""): + default: +} + +var x = isString1(0, "") === isString2(""); + +function isString3(a: number, b: number, c: Object): c is string { + return isString1(0, c); +} + + +//// [typeGuardOfFormFunctionEquality.js] +switch (isString1(0, "")) { + case isString2(""): + default: +} +var x = isString1(0, "") === isString2(""); +function isString3(a, b, c) { + return isString1(0, c); +} diff --git a/tests/baselines/reference/typePredicateOnVariableDeclaration01.js b/tests/baselines/reference/typePredicateOnVariableDeclaration01.js new file mode 100644 index 00000000000..5831142cf5c --- /dev/null +++ b/tests/baselines/reference/typePredicateOnVariableDeclaration01.js @@ -0,0 +1,10 @@ +//// [typePredicateOnVariableDeclaration01.ts] + +var x: this is string; + +//// [typePredicateOnVariableDeclaration01.js] +var x; + + +//// [typePredicateOnVariableDeclaration01.d.ts] +declare var x: this is string; diff --git a/tests/baselines/reference/typePredicateOnVariableDeclaration02.js b/tests/baselines/reference/typePredicateOnVariableDeclaration02.js new file mode 100644 index 00000000000..b28a89116ba --- /dev/null +++ b/tests/baselines/reference/typePredicateOnVariableDeclaration02.js @@ -0,0 +1,6 @@ +//// [typePredicateOnVariableDeclaration02.ts] + +var y: z is number; + +//// [typePredicateOnVariableDeclaration02.js] +var y = is, number; From 91ea83c72a07dbb0471d2a8e5ba6537e82b0b319 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Sat, 13 Feb 2016 02:51:25 +0000 Subject: [PATCH 41/55] Addressed PR --- src/compiler/checker.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d6b67c456ca..533fffda805 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14075,12 +14075,12 @@ namespace ts { } function checkBaseTypeAccessibility(type: ObjectType, node: ExpressionWithTypeArguments) { - const typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); - if (!isNodeWithinClass(node, typeClassDeclaration)) { - const signatures = getSignaturesOfType(type, SignatureKind.Construct); - if (signatures.length) { - const declaration = signatures[0].declaration; - if (declaration && declaration.flags & NodeFlags.Private) { + const signatures = getSignaturesOfType(type, SignatureKind.Construct); + if (signatures.length) { + const declaration = signatures[0].declaration; + if (declaration && declaration.flags & NodeFlags.Private) { + const typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); + if (!isNodeWithinClass(node, typeClassDeclaration)) { error(node, Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, (node.expression).text); } } From 208830efa3bde4c6c0b2595e3cc085c237424df6 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Sat, 13 Feb 2016 02:52:25 +0000 Subject: [PATCH 42/55] Updated tests and accepted baseline --- .../classConstructorAccessibility4.errors.txt | 42 -------------- .../classConstructorAccessibility4.symbols | 53 ++++++++++++++++++ .../classConstructorAccessibility4.types | 55 +++++++++++++++++++ .../classConstructorAccessibility4.ts | 8 +-- 4 files changed, 111 insertions(+), 47 deletions(-) delete mode 100644 tests/baselines/reference/classConstructorAccessibility4.errors.txt create mode 100644 tests/baselines/reference/classConstructorAccessibility4.symbols create mode 100644 tests/baselines/reference/classConstructorAccessibility4.types diff --git a/tests/baselines/reference/classConstructorAccessibility4.errors.txt b/tests/baselines/reference/classConstructorAccessibility4.errors.txt deleted file mode 100644 index 1cc50e3f4c8..00000000000 --- a/tests/baselines/reference/classConstructorAccessibility4.errors.txt +++ /dev/null @@ -1,42 +0,0 @@ -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts(16,9): error TS2673: Constructor of class 'A' is private and only accessible within the class declaration. -tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts(33,9): error TS2674: Constructor of class 'D' is protected and only accessible within the class declaration. - - -==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts (2 errors) ==== - class A { - private constructor() { } - - method() { - class B { - method() { - new A(); // OK - } - } - - class C extends A { // OK - } - } - } - - var a = new A(); // error - ~~~~~~~ -!!! error TS2673: Constructor of class 'A' is private and only accessible within the class declaration. - - class D { - protected constructor() { } - - method() { - class E { - method() { - new D(); // OK - } - } - - class F extends D { // OK - } - } - } - - var d = new D(); // error - ~~~~~~~ -!!! error TS2674: Constructor of class 'D' is protected and only accessible within the class declaration. \ No newline at end of file diff --git a/tests/baselines/reference/classConstructorAccessibility4.symbols b/tests/baselines/reference/classConstructorAccessibility4.symbols new file mode 100644 index 00000000000..e3d8301ca68 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility4.symbols @@ -0,0 +1,53 @@ +=== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts === + +class A { +>A : Symbol(A, Decl(classConstructorAccessibility4.ts, 0, 0)) + + private constructor() { } + + method() { +>method : Symbol(method, Decl(classConstructorAccessibility4.ts, 2, 29)) + + class B { +>B : Symbol(B, Decl(classConstructorAccessibility4.ts, 4, 14)) + + method() { +>method : Symbol(method, Decl(classConstructorAccessibility4.ts, 5, 17)) + + new A(); // OK +>A : Symbol(A, Decl(classConstructorAccessibility4.ts, 0, 0)) + } + } + + class C extends A { // OK +>C : Symbol(C, Decl(classConstructorAccessibility4.ts, 9, 9)) +>A : Symbol(A, Decl(classConstructorAccessibility4.ts, 0, 0)) + } + } +} + +class D { +>D : Symbol(D, Decl(classConstructorAccessibility4.ts, 14, 1)) + + protected constructor() { } + + method() { +>method : Symbol(method, Decl(classConstructorAccessibility4.ts, 17, 31)) + + class E { +>E : Symbol(E, Decl(classConstructorAccessibility4.ts, 19, 14)) + + method() { +>method : Symbol(method, Decl(classConstructorAccessibility4.ts, 20, 17)) + + new D(); // OK +>D : Symbol(D, Decl(classConstructorAccessibility4.ts, 14, 1)) + } + } + + class F extends D { // OK +>F : Symbol(F, Decl(classConstructorAccessibility4.ts, 24, 9)) +>D : Symbol(D, Decl(classConstructorAccessibility4.ts, 14, 1)) + } + } +} diff --git a/tests/baselines/reference/classConstructorAccessibility4.types b/tests/baselines/reference/classConstructorAccessibility4.types new file mode 100644 index 00000000000..1eb5a25e220 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility4.types @@ -0,0 +1,55 @@ +=== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts === + +class A { +>A : A + + private constructor() { } + + method() { +>method : () => void + + class B { +>B : B + + method() { +>method : () => void + + new A(); // OK +>new A() : A +>A : typeof A + } + } + + class C extends A { // OK +>C : C +>A : A + } + } +} + +class D { +>D : D + + protected constructor() { } + + method() { +>method : () => void + + class E { +>E : E + + method() { +>method : () => void + + new D(); // OK +>new D() : D +>D : typeof D + } + } + + class F extends D { // OK +>F : F +>D : D + } + } +} diff --git a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts index e75f9be6fde..3760f2176ae 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility4.ts @@ -1,3 +1,5 @@ +// @declaration: true + class A { private constructor() { } @@ -13,8 +15,6 @@ class A { } } -var a = new A(); // error - class D { protected constructor() { } @@ -28,6 +28,4 @@ class D { class F extends D { // OK } } -} - -var d = new D(); // error \ No newline at end of file +} \ No newline at end of file From f601e6dd760b667c50298b2c0d334cd7a2d06d52 Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Wed, 3 Feb 2016 12:54:48 -0800 Subject: [PATCH 43/55] Allow decorators in JavaScript files --- src/compiler/diagnosticMessages.json | 5 ----- src/compiler/program.ts | 3 --- .../jsFileCompilationDecoratorSyntax.errors.txt | 9 --------- .../reference/jsFileCompilationDecoratorSyntax.symbols | 4 ++++ .../reference/jsFileCompilationDecoratorSyntax.types | 5 +++++ .../cases/compiler/jsFileCompilationDecoratorSyntax.ts | 1 + .../fourslash/getJavaScriptSemanticDiagnostics21.ts | 10 +--------- 7 files changed, 11 insertions(+), 26 deletions(-) delete mode 100644 tests/baselines/reference/jsFileCompilationDecoratorSyntax.errors.txt create mode 100644 tests/baselines/reference/jsFileCompilationDecoratorSyntax.symbols create mode 100644 tests/baselines/reference/jsFileCompilationDecoratorSyntax.types diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 25b86e67d34..a34a7b02c3b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2729,11 +2729,6 @@ "category": "Error", "code": 8016 }, - "'decorators' can only be used in a .ts file.": { - "category": "Error", - "code": 8017 - }, - "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses.": { "category": "Error", "code": 9002 diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 7a2e65ed33b..bfea431d9ad 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1168,9 +1168,6 @@ namespace ts { let typeAssertionExpression = node; diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file)); return true; - case SyntaxKind.Decorator: - diagnostics.push(createDiagnosticForNode(node, Diagnostics.decorators_can_only_be_used_in_a_ts_file)); - return true; } return forEachChild(node, walk); diff --git a/tests/baselines/reference/jsFileCompilationDecoratorSyntax.errors.txt b/tests/baselines/reference/jsFileCompilationDecoratorSyntax.errors.txt deleted file mode 100644 index a39d2e1665c..00000000000 --- a/tests/baselines/reference/jsFileCompilationDecoratorSyntax.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. -tests/cases/compiler/a.js(1,1): error TS8017: 'decorators' can only be used in a .ts file. - - -!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. -==== tests/cases/compiler/a.js (1 errors) ==== - @internal class C { } - ~~~~~~~~~ -!!! error TS8017: 'decorators' can only be used in a .ts file. \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationDecoratorSyntax.symbols b/tests/baselines/reference/jsFileCompilationDecoratorSyntax.symbols new file mode 100644 index 00000000000..02973b43147 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationDecoratorSyntax.symbols @@ -0,0 +1,4 @@ +=== tests/cases/compiler/a.js === +@internal class C { } +>C : Symbol(C, Decl(a.js, 0, 0)) + diff --git a/tests/baselines/reference/jsFileCompilationDecoratorSyntax.types b/tests/baselines/reference/jsFileCompilationDecoratorSyntax.types new file mode 100644 index 00000000000..ee1f14129d5 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationDecoratorSyntax.types @@ -0,0 +1,5 @@ +=== tests/cases/compiler/a.js === +@internal class C { } +>internal : any +>C : C + diff --git a/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts b/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts index 2ef95db01fe..ece41012dfe 100644 --- a/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts +++ b/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts @@ -1,3 +1,4 @@ // @allowJs: true +// @noEmit: true // @filename: a.js @internal class C { } \ No newline at end of file diff --git a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts index b71677554a2..04dae8b592b 100644 --- a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts +++ b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts @@ -4,12 +4,4 @@ // @Filename: a.js //// @internal class C {} -verify.getSemanticDiagnostics(`[ - { - "message": "'decorators' can only be used in a .ts file.", - "start": 0, - "length": 9, - "category": "error", - "code": 8017 - } -]`); \ No newline at end of file +verify.getSemanticDiagnostics(`[]`); From ed7abcc1e9ac32a637eafaf5befbf9d060b41c1e Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Fri, 12 Feb 2016 19:19:23 -0800 Subject: [PATCH 44/55] Set experimentalDecorators warning for JavaScript --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/program.ts | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6b43b3802c2..d385d9cff74 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12740,7 +12740,7 @@ namespace ts { } if (!compilerOptions.experimentalDecorators) { - error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Specify_experimentalDecorators_to_remove_this_warning); + error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_experimentalDecorators_to_remove_this_warning); } if (compilerOptions.emitDecoratorMetadata) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index a34a7b02c3b..40235bc1710 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -687,7 +687,7 @@ "category": "Error", "code": 1218 }, - "Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning.": { + "Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning.": { "category": "Error", "code": 1219 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index bfea431d9ad..00f8fb441d5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1168,6 +1168,11 @@ namespace ts { let typeAssertionExpression = node; diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file)); return true; + case SyntaxKind.Decorator: + if (!options.experimentalDecorators) { + diagnostics.push(createDiagnosticForNode(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_experimentalDecorators_to_remove_this_warning)) + } + return true; } return forEachChild(node, walk); From fb474d13dde2eb6a0747edee7ffe63ed47967d41 Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Fri, 12 Feb 2016 19:35:05 -0800 Subject: [PATCH 45/55] Updated tests --- tests/baselines/reference/generatorTypeCheck39.errors.txt | 4 ++-- tests/baselines/reference/generatorTypeCheck59.errors.txt | 4 ++-- tests/baselines/reference/generatorTypeCheck61.errors.txt | 4 ++-- tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts | 3 ++- tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts | 1 + 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/baselines/reference/generatorTypeCheck39.errors.txt b/tests/baselines/reference/generatorTypeCheck39.errors.txt index ae24837f712..c9d916f63e9 100644 --- a/tests/baselines/reference/generatorTypeCheck39.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck39.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,16): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(6,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(6,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(7,13): error TS1163: A 'yield' expression is only allowed in a generator body. @@ -13,7 +13,7 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(7,13): erro !!! error TS1163: A 'yield' expression is only allowed in a generator body. class C { ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. x = yield 0; ~~~~~ !!! error TS1163: A 'yield' expression is only allowed in a generator body. diff --git a/tests/baselines/reference/generatorTypeCheck59.errors.txt b/tests/baselines/reference/generatorTypeCheck59.errors.txt index d8179803821..4ba93f10ae0 100644 --- a/tests/baselines/reference/generatorTypeCheck59.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck59.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(3,11): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts (2 errors) ==== @@ -10,6 +10,6 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error !!! error TS1163: A 'yield' expression is only allowed in a generator body. m() { } ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. }; } \ No newline at end of file diff --git a/tests/baselines/reference/generatorTypeCheck61.errors.txt b/tests/baselines/reference/generatorTypeCheck61.errors.txt index b38e96d31ce..2ecfe49973d 100644 --- a/tests/baselines/reference/generatorTypeCheck61.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck61.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(2,7): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts (2 errors) ==== @@ -9,5 +9,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): erro !!! error TS1163: A 'yield' expression is only allowed in a generator body. class C {}; ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. } \ No newline at end of file diff --git a/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts b/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts index ece41012dfe..6627fb5dca0 100644 --- a/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts +++ b/tests/cases/compiler/jsFileCompilationDecoratorSyntax.ts @@ -1,4 +1,5 @@ // @allowJs: true // @noEmit: true +// @experimentalDecorators: true // @filename: a.js -@internal class C { } \ No newline at end of file +@internal class C { } diff --git a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts index 04dae8b592b..8a7120acbb3 100644 --- a/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts +++ b/tests/cases/fourslash/getJavaScriptSemanticDiagnostics21.ts @@ -1,6 +1,7 @@ /// // @allowJs: true +// @experimentalDecorators: true // @Filename: a.js //// @internal class C {} From fe60490c2a0c7b6b1eea145619593d0b489f056e Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Sat, 13 Feb 2016 08:02:16 -0800 Subject: [PATCH 46/55] Fixed wording and updated tests --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/program.ts | 2 +- tests/baselines/reference/generatorTypeCheck39.errors.txt | 4 ++-- tests/baselines/reference/generatorTypeCheck59.errors.txt | 4 ++-- tests/baselines/reference/generatorTypeCheck61.errors.txt | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d385d9cff74..7b9cc76b11b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12740,7 +12740,7 @@ namespace ts { } if (!compilerOptions.experimentalDecorators) { - error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_experimentalDecorators_to_remove_this_warning); + error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); } if (compilerOptions.emitDecoratorMetadata) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 40235bc1710..1829e99eb16 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -687,7 +687,7 @@ "category": "Error", "code": 1218 }, - "Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning.": { + "Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.": { "category": "Error", "code": 1219 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 00f8fb441d5..a51b3eed4bf 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1170,7 +1170,7 @@ namespace ts { return true; case SyntaxKind.Decorator: if (!options.experimentalDecorators) { - diagnostics.push(createDiagnosticForNode(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_experimentalDecorators_to_remove_this_warning)) + diagnostics.push(createDiagnosticForNode(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning)); } return true; } diff --git a/tests/baselines/reference/generatorTypeCheck39.errors.txt b/tests/baselines/reference/generatorTypeCheck39.errors.txt index c9d916f63e9..aabb5a47a35 100644 --- a/tests/baselines/reference/generatorTypeCheck39.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck39.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,16): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(6,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(6,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(7,13): error TS1163: A 'yield' expression is only allowed in a generator body. @@ -13,7 +13,7 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(7,13): erro !!! error TS1163: A 'yield' expression is only allowed in a generator body. class C { ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. x = yield 0; ~~~~~ !!! error TS1163: A 'yield' expression is only allowed in a generator body. diff --git a/tests/baselines/reference/generatorTypeCheck59.errors.txt b/tests/baselines/reference/generatorTypeCheck59.errors.txt index 4ba93f10ae0..cba90154701 100644 --- a/tests/baselines/reference/generatorTypeCheck59.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck59.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(3,11): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts (2 errors) ==== @@ -10,6 +10,6 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck59.ts(4,9): error !!! error TS1163: A 'yield' expression is only allowed in a generator body. m() { } ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. }; } \ No newline at end of file diff --git a/tests/baselines/reference/generatorTypeCheck61.errors.txt b/tests/baselines/reference/generatorTypeCheck61.errors.txt index 2ecfe49973d..ccc99f7ffbd 100644 --- a/tests/baselines/reference/generatorTypeCheck61.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck61.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(2,7): error TS1163: A 'yield' expression is only allowed in a generator body. -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts (2 errors) ==== @@ -9,5 +9,5 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck61.ts(3,11): erro !!! error TS1163: A 'yield' expression is only allowed in a generator body. class C {}; ~ -!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set 'experimentalDecorators' to remove this warning. +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. } \ No newline at end of file From ba8b1680cbf5fa5763fe1d6000a36ac05d8bdb43 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Sun, 14 Feb 2016 21:16:12 +0000 Subject: [PATCH 47/55] Included previously ignored baseline .js file and slight refactoring --- src/compiler/checker.ts | 7 +- .../classConstructorAccessibility4.js | 93 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/classConstructorAccessibility4.js diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 533fffda805..f775f562695 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15412,14 +15412,13 @@ namespace ts { function isNodeWithinClass(node: Node, classDeclaration: ClassLikeDeclaration) { while (true) { - const containingClass = getContainingClass(node); - if (!containingClass) { + node = getContainingClass(node); + if (!node) { return false; } - if (containingClass === classDeclaration) { + if (node === classDeclaration) { return true; } - node = containingClass; } } diff --git a/tests/baselines/reference/classConstructorAccessibility4.js b/tests/baselines/reference/classConstructorAccessibility4.js new file mode 100644 index 00000000000..6342facae21 --- /dev/null +++ b/tests/baselines/reference/classConstructorAccessibility4.js @@ -0,0 +1,93 @@ +//// [classConstructorAccessibility4.ts] + +class A { + private constructor() { } + + method() { + class B { + method() { + new A(); // OK + } + } + + class C extends A { // OK + } + } +} + +class D { + protected constructor() { } + + method() { + class E { + method() { + new D(); // OK + } + } + + class F extends D { // OK + } + } +} + +//// [classConstructorAccessibility4.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + A.prototype.method = function () { + var B = (function () { + function B() { + } + B.prototype.method = function () { + new A(); // OK + }; + return B; + }()); + var C = (function (_super) { + __extends(C, _super); + function C() { + _super.apply(this, arguments); + } + return C; + }(A)); + }; + return A; +}()); +var D = (function () { + function D() { + } + D.prototype.method = function () { + var E = (function () { + function E() { + } + E.prototype.method = function () { + new D(); // OK + }; + return E; + }()); + var F = (function (_super) { + __extends(F, _super); + function F() { + _super.apply(this, arguments); + } + return F; + }(D)); + }; + return D; +}()); + + +//// [classConstructorAccessibility4.d.ts] +declare class A { + private constructor(); + method(): void; +} +declare class D { + protected constructor(); + method(): void; +} From eed65a0334337de99da9de824b738a097a57138f Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 16 Feb 2016 22:01:28 -0800 Subject: [PATCH 48/55] Port #7106 to master --- src/compiler/emitter.ts | 2 +- src/compiler/program.ts | 20 ++++++++++++--- src/compiler/tsc.ts | 25 ++++++------------- src/compiler/types.ts | 2 +- src/harness/projectsRunner.ts | 2 +- .../baselines/reference/APISample_compile.js | 4 +-- tests/cases/compiler/APISample_compile.ts | 2 +- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f08082d72c8..d2c96a11774 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -389,7 +389,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge return { emitSkipped, - diagnostics: emitterDiagnostics.getDiagnostics(), + declarationDiagnostics: emitterDiagnostics.getDiagnostics(), sourceMaps: sourceMapDataList }; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 9aee98a10ec..f29784ccf3c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -953,13 +953,27 @@ namespace ts { } function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken): EmitResult { + let declarationDiagnostics: Diagnostic[] = []; + + if (options.noEmit) { + return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; + } + // If the noEmitOnError flag is set, then check if we have any errors so far. If so, // immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we // get any preEmit diagnostics, not just the ones if (options.noEmitOnError) { - const preEmitDiagnostics = getPreEmitDiagnostics(program, /*sourceFile:*/ undefined, cancellationToken); - if (preEmitDiagnostics.length > 0) { - return { diagnostics: preEmitDiagnostics, sourceMaps: undefined, emitSkipped: true }; + let diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( + program.getSyntacticDiagnostics(sourceFile, cancellationToken), + program.getGlobalDiagnostics(cancellationToken), + program.getSemanticDiagnostics(sourceFile, cancellationToken)); + + if (diagnostics.length === 0 && program.getCompilerOptions().declaration) { + declarationDiagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken); + } + + if (diagnostics.length > 0 || declarationDiagnostics.length > 0) { + return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; } } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index c69bfca1877..59212ed849b 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -590,30 +590,21 @@ namespace ts { } } - reportDiagnostics(diagnostics, compilerHost); - - // If the user doesn't want us to emit, then we're done at this point. - if (compilerOptions.noEmit) { - return diagnostics.length - ? ExitStatus.DiagnosticsPresent_OutputsSkipped - : ExitStatus.Success; - } - // Otherwise, emit and report any errors we ran into. const emitOutput = program.emit(); - reportDiagnostics(emitOutput.diagnostics, compilerHost); + diagnostics = diagnostics.concat(emitOutput.declarationDiagnostics); - // If the emitter didn't emit anything, then pass that value along. - if (emitOutput.emitSkipped) { + reportDiagnostics(sortAndDeduplicateDiagnostics(diagnostics), compilerHost); + + if (emitOutput.emitSkipped && diagnostics.length > 0) { + // If the emitter didn't emit anything, then pass that value along. return ExitStatus.DiagnosticsPresent_OutputsSkipped; } - - // The emitter emitted something, inform the caller if that happened in the presence - // of diagnostics or not. - if (diagnostics.length > 0 || emitOutput.diagnostics.length > 0) { + else if (diagnostics.length > 0) { + // The emitter emitted something, inform the caller if that happened in the presence + // of diagnostics or not. return ExitStatus.DiagnosticsPresent_OutputsGenerated; } - return ExitStatus.Success; } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d7d7518c886..e8fca86c4b2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1680,7 +1680,7 @@ namespace ts { export interface EmitResult { emitSkipped: boolean; - diagnostics: Diagnostic[]; + /* @internal */ declarationDiagnostics: Diagnostic[]; /* @internal */ sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps } diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 96fcfedc6b4..b8280ed24b9 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -127,7 +127,7 @@ class ProjectRunner extends RunnerBase { let errors = ts.getPreEmitDiagnostics(program); const emitResult = program.emit(); - errors = ts.concatenate(errors, emitResult.diagnostics); + errors = ts.concatenate(errors, emitResult.declarationDiagnostics); const sourceMapData = emitResult.sourceMaps; // Clean up source map data that will be used in baselining diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index 3830e53697c..5f55c04c9f9 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -16,7 +16,7 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); @@ -45,7 +45,7 @@ var ts = require("typescript"); function compile(fileNames, options) { var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(function (diagnostic) { var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); diff --git a/tests/cases/compiler/APISample_compile.ts b/tests/cases/compiler/APISample_compile.ts index c63009f7d63..9672b9863fa 100644 --- a/tests/cases/compiler/APISample_compile.ts +++ b/tests/cases/compiler/APISample_compile.ts @@ -18,7 +18,7 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); From 2b52ae1cb7fd65392f1e5b4d3c41dee7a9180d84 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 16 Feb 2016 22:57:27 -0800 Subject: [PATCH 49/55] Fix lint errors --- src/compiler/program.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index f29784ccf3c..2b1a38d2084 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -963,7 +963,7 @@ namespace ts { // immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we // get any preEmit diagnostics, not just the ones if (options.noEmitOnError) { - let diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( + const diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( program.getSyntacticDiagnostics(sourceFile, cancellationToken), program.getGlobalDiagnostics(cancellationToken), program.getSemanticDiagnostics(sourceFile, cancellationToken)); From 5e770bda2e30443c052e8a6483e25509e609c59c Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 17 Feb 2016 10:19:20 -0800 Subject: [PATCH 50/55] correctly check exported type aliases merged with overloads --- src/compiler/checker.ts | 19 ++++++++++++++----- .../exportRedeclarationTypeAliases.js | 9 +++++++++ .../exportRedeclarationTypeAliases.symbols | 10 ++++++++++ .../exportRedeclarationTypeAliases.types | 10 ++++++++++ .../exportRedeclarationTypeAliases.ts | 4 ++++ 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/exportRedeclarationTypeAliases.js create mode 100644 tests/baselines/reference/exportRedeclarationTypeAliases.symbols create mode 100644 tests/baselines/reference/exportRedeclarationTypeAliases.types create mode 100644 tests/cases/compiler/exportRedeclarationTypeAliases.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ab057d430a4..d829795439b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15055,11 +15055,20 @@ namespace ts { continue; } const { declarations, flags } = exports[id]; - // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. (TS Exceptions: namespaces, function overloads, enums, and interfaces) - if (!(flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) && (flags & SymbolFlags.TypeAlias ? declarations.length - 1 : declarations.length) > 1) { - const exportedDeclarations: Declaration[] = filter(declarations, isNotOverload); - if (exportedDeclarations.length > 1) { - for (const declaration of exportedDeclarations) { + // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. + // (TS Exceptions: namespaces, function overloads, enums, and interfaces) + if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) { + continue; + } + const exportedDeclarationsCount = countWhere(declarations, isNotOverload); + if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) { + // it is legal to merge type alias with other values + // so count should be either 1 (just type alias) or 2 (type alias + merged value) + continue; + } + if (exportedDeclarationsCount > 1) { + for (const declaration of declarations) { + if (isNotOverload(declaration)) { diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, id)); } } diff --git a/tests/baselines/reference/exportRedeclarationTypeAliases.js b/tests/baselines/reference/exportRedeclarationTypeAliases.js new file mode 100644 index 00000000000..b1e2e200634 --- /dev/null +++ b/tests/baselines/reference/exportRedeclarationTypeAliases.js @@ -0,0 +1,9 @@ +//// [exportRedeclarationTypeAliases.ts] +export type Foo = number; +export function Foo(): number; +export function Foo(): any {} + +//// [exportRedeclarationTypeAliases.js] +"use strict"; +function Foo() { } +exports.Foo = Foo; diff --git a/tests/baselines/reference/exportRedeclarationTypeAliases.symbols b/tests/baselines/reference/exportRedeclarationTypeAliases.symbols new file mode 100644 index 00000000000..6561692e307 --- /dev/null +++ b/tests/baselines/reference/exportRedeclarationTypeAliases.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/exportRedeclarationTypeAliases.ts === +export type Foo = number; +>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30)) + +export function Foo(): number; +>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30)) + +export function Foo(): any {} +>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30)) + diff --git a/tests/baselines/reference/exportRedeclarationTypeAliases.types b/tests/baselines/reference/exportRedeclarationTypeAliases.types new file mode 100644 index 00000000000..f5c987373b6 --- /dev/null +++ b/tests/baselines/reference/exportRedeclarationTypeAliases.types @@ -0,0 +1,10 @@ +=== tests/cases/compiler/exportRedeclarationTypeAliases.ts === +export type Foo = number; +>Foo : number + +export function Foo(): number; +>Foo : () => number + +export function Foo(): any {} +>Foo : () => number + diff --git a/tests/cases/compiler/exportRedeclarationTypeAliases.ts b/tests/cases/compiler/exportRedeclarationTypeAliases.ts new file mode 100644 index 00000000000..eb5cbcd4c6d --- /dev/null +++ b/tests/cases/compiler/exportRedeclarationTypeAliases.ts @@ -0,0 +1,4 @@ +// @module: commonjs +export type Foo = number; +export function Foo(): number; +export function Foo(): any {} \ No newline at end of file From bebc3a64b83cb5ece2472c470924362cf688a9a1 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Wed, 17 Feb 2016 11:09:17 -0800 Subject: [PATCH 51/55] Add issue and PR templates --- issue_template.md | 23 +++++++++++++++++++++++ pull_request_template.md | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 issue_template.md create mode 100644 pull_request_template.md diff --git a/issue_template.md b/issue_template.md new file mode 100644 index 00000000000..6be508efec2 --- /dev/null +++ b/issue_template.md @@ -0,0 +1,23 @@ + + +**TypeScript Version:** + +**Code** +```ts +// A self-contained demonstration of the problem follows... + +``` + +**Expected behavior:** +**Actual behavior:** diff --git a/pull_request_template.md b/pull_request_template.md new file mode 100644 index 00000000000..2baaa3521dd --- /dev/null +++ b/pull_request_template.md @@ -0,0 +1,16 @@ + + +**Fixes issue:** # From ed8cf666f672c729060935e90a1e5838e9407d0d Mon Sep 17 00:00:00 2001 From: falsandtru Date: Thu, 18 Feb 2016 04:51:47 +0900 Subject: [PATCH 52/55] Add version examples --- issue_template.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/issue_template.md b/issue_template.md index 6be508efec2..84de786b112 100644 --- a/issue_template.md +++ b/issue_template.md @@ -13,11 +13,15 @@ __________________________________________________________ --> **TypeScript Version:** +1.7.5 / 1.8.0-beta / master() + **Code** + ```ts // A self-contained demonstration of the problem follows... ``` -**Expected behavior:** +**Expected behavior:** + **Actual behavior:** From 50829b29d2660e6a7ec8a168818eba5ae4c009de Mon Sep 17 00:00:00 2001 From: falsandtru Date: Thu, 18 Feb 2016 05:06:12 +0900 Subject: [PATCH 53/55] Modify version format --- issue_template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/issue_template.md b/issue_template.md index 84de786b112..dcd2280570c 100644 --- a/issue_template.md +++ b/issue_template.md @@ -13,7 +13,7 @@ __________________________________________________________ --> **TypeScript Version:** -1.7.5 / 1.8.0-beta / master() +1.7.5 / 1.8.0-beta / nightly (1.9.0-dev.20160217) **Code** From 353998a6bd79be1301699318bb9d3eca1cc745e9 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 18 Feb 2016 12:06:17 -0800 Subject: [PATCH 54/55] Rever change to EmitOutput.diagnostics --- src/compiler/emitter.ts | 2 +- src/compiler/program.ts | 4 ++-- src/compiler/tsc.ts | 2 +- src/compiler/types.ts | 3 ++- src/harness/projectsRunner.ts | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d2c96a11774..f08082d72c8 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -389,7 +389,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge return { emitSkipped, - declarationDiagnostics: emitterDiagnostics.getDiagnostics(), + diagnostics: emitterDiagnostics.getDiagnostics(), sourceMaps: sourceMapDataList }; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2b1a38d2084..d41f34df0b3 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -956,7 +956,7 @@ namespace ts { let declarationDiagnostics: Diagnostic[] = []; if (options.noEmit) { - return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; + return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; } // If the noEmitOnError flag is set, then check if we have any errors so far. If so, @@ -973,7 +973,7 @@ namespace ts { } if (diagnostics.length > 0 || declarationDiagnostics.length > 0) { - return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; + return { diagnostics, sourceMaps: undefined, emitSkipped: true }; } } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 59212ed849b..8e08091bc77 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -592,7 +592,7 @@ namespace ts { // Otherwise, emit and report any errors we ran into. const emitOutput = program.emit(); - diagnostics = diagnostics.concat(emitOutput.declarationDiagnostics); + diagnostics = diagnostics.concat(emitOutput.diagnostics); reportDiagnostics(sortAndDeduplicateDiagnostics(diagnostics), compilerHost); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e8fca86c4b2..58a151a2bc0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1680,7 +1680,8 @@ namespace ts { export interface EmitResult { emitSkipped: boolean; - /* @internal */ declarationDiagnostics: Diagnostic[]; + /** Contains declaration emit diagnostics */ + diagnostics: Diagnostic[]; /* @internal */ sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps } diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index b8280ed24b9..96fcfedc6b4 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -127,7 +127,7 @@ class ProjectRunner extends RunnerBase { let errors = ts.getPreEmitDiagnostics(program); const emitResult = program.emit(); - errors = ts.concatenate(errors, emitResult.declarationDiagnostics); + errors = ts.concatenate(errors, emitResult.diagnostics); const sourceMapData = emitResult.sourceMaps; // Clean up source map data that will be used in baselining From 46cc470332d27fbbfd0c4ace7085b3439e60e5cf Mon Sep 17 00:00:00 2001 From: Tingan Ho Date: Fri, 19 Feb 2016 08:25:23 +0800 Subject: [PATCH 55/55] Fixes exclude options --- src/compiler/tsc.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 8e08091bc77..e5b85a7391e 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -710,14 +710,16 @@ namespace ts { else { const compilerOptions = extend(options, defaultInitCompilerOptions); const configurations: any = { - compilerOptions: serializeCompilerOptions(compilerOptions), - exclude: ["node_modules"] + compilerOptions: serializeCompilerOptions(compilerOptions) }; if (fileNames && fileNames.length) { // only set the files property if we have at least one file configurations.files = fileNames; } + else { + configurations.exclude = ["node_modules"]; + } sys.writeFile(file, JSON.stringify(configurations, undefined, 4)); reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file), /* compilerHost */ undefined);