Skip to content

Commit

Permalink
Merge pull request #8389 from Microsoft/make-object-literal-this-any-…
Browse files Browse the repository at this point in the history
…typed

Make `this` in object literal always of type `any`
  • Loading branch information
sandersn committed Apr 30, 2016
2 parents f0e2d81 + 8d45a73 commit d1b60f8
Show file tree
Hide file tree
Showing 33 changed files with 156 additions and 206 deletions.
14 changes: 1 addition & 13 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8228,15 +8228,6 @@ namespace ts {
if (signature.thisType) {
return signature.thisType;
}
const parentObject = container.parent && container.parent.kind === SyntaxKind.PropertyAssignment ? container.parent.parent : container.parent;
if (parentObject && parentObject.kind === SyntaxKind.ObjectLiteralExpression) {
// Note: this works because object literal methods are deferred,
// which means that the type of the containing object literal is already known.
const type = checkExpressionCached(<ObjectLiteralExpression>parentObject);
if (type) {
return type;
}
}
}
if (isClassLike(container.parent)) {
const symbol = getSymbolOfNode(container.parent);
Expand Down Expand Up @@ -8476,10 +8467,7 @@ namespace ts {
if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== SyntaxKind.ArrowFunction) {
const contextualSignature = getContextualSignature(func);
if (contextualSignature) {
return contextualSignature.thisType || anyType;
}
else if (getContextualTypeForFunctionLikeDeclaration(func) === anyType) {
return anyType;
return contextualSignature.thisType;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
tests/cases/compiler/commentsOnObjectLiteral2.ts(1,14): error TS2304: Cannot find name 'makeClass'.
tests/cases/compiler/commentsOnObjectLiteral2.ts(9,17): error TS2339: Property 'name' does not exist on type '{ initialize: (name: any) => void; }'.


==== tests/cases/compiler/commentsOnObjectLiteral2.ts (2 errors) ====
==== tests/cases/compiler/commentsOnObjectLiteral2.ts (1 errors) ====
var Person = makeClass(
~~~~~~~~~
!!! error TS2304: Cannot find name 'makeClass'.
Expand All @@ -14,8 +13,6 @@ tests/cases/compiler/commentsOnObjectLiteral2.ts(9,17): error TS2339: Property '
*/
initialize: function(name) {
this.name = name;
~~~~
!!! error TS2339: Property 'name' does not exist on type '{ initialize: (name: any) => void; }'.
} /* trailing comment 1*/,
}
);
7 changes: 0 additions & 7 deletions tests/baselines/reference/commentsOnObjectLiteral3.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@ var v = {
>a : Symbol(a, Decl(commentsOnObjectLiteral3.ts, 8, 13), Decl(commentsOnObjectLiteral3.ts, 12, 18))

return this.prop;
>this.prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
>this : Symbol(, Decl(commentsOnObjectLiteral3.ts, 1, 7))
>prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))

} /*trailing 1*/,
//setter
set a(value) {
>a : Symbol(a, Decl(commentsOnObjectLiteral3.ts, 8, 13), Decl(commentsOnObjectLiteral3.ts, 12, 18))
>value : Symbol(value, Decl(commentsOnObjectLiteral3.ts, 14, 7))

this.prop = value;
>this.prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
>this : Symbol(, Decl(commentsOnObjectLiteral3.ts, 1, 7))
>prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
>value : Symbol(value, Decl(commentsOnObjectLiteral3.ts, 14, 7))

} // trailing 2
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/commentsOnObjectLiteral3.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ var v = {
>a : any

return this.prop;
>this.prop : number
>this : { prop: number; func: () => void; func1(): void; a: any; }
>prop : number
>this.prop : any
>this : any
>prop : any

} /*trailing 1*/,
//setter
Expand All @@ -36,9 +36,9 @@ var v = {

this.prop = value;
>this.prop = value : any
>this.prop : number
>this : { prop: number; func: () => void; func1(): void; a: any; }
>prop : number
>this.prop : any
>this : any
>prop : any
>value : any

} // trailing 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ function /*1*/makePoint(x: number) {
set x(a: number) { this.b = a; }
>x : Symbol(x, Decl(declFileObjectLiteralWithAccessors.ts, 3, 14), Decl(declFileObjectLiteralWithAccessors.ts, 4, 30))
>a : Symbol(a, Decl(declFileObjectLiteralWithAccessors.ts, 5, 14))
>this.b : Symbol(b, Decl(declFileObjectLiteralWithAccessors.ts, 2, 12))
>this : Symbol(, Decl(declFileObjectLiteralWithAccessors.ts, 2, 10))
>b : Symbol(b, Decl(declFileObjectLiteralWithAccessors.ts, 2, 12))
>a : Symbol(a, Decl(declFileObjectLiteralWithAccessors.ts, 5, 14))

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function /*1*/makePoint(x: number) {
>x : number
>a : number
>this.b = a : number
>this.b : number
>this : { b: number; x: number; }
>b : number
>this.b : any
>this : any
>b : any
>a : number

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ function /*1*/makePoint(x: number) {
set x(a: number) { this.b = a; }
>x : Symbol(x, Decl(declFileObjectLiteralWithOnlySetter.ts, 3, 14))
>a : Symbol(a, Decl(declFileObjectLiteralWithOnlySetter.ts, 4, 14))
>this.b : Symbol(b, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 12))
>this : Symbol(, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 10))
>b : Symbol(b, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 12))
>a : Symbol(a, Decl(declFileObjectLiteralWithOnlySetter.ts, 4, 14))

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function /*1*/makePoint(x: number) {
>x : number
>a : number
>this.b = a : number
>this.b : number
>this : { b: number; x: number; }
>b : number
>this.b : any
>this : any
>b : any
>a : number

};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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(10,19): error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
Property 'a' is missing in type '{ m(): this is Foo; }'.


==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts (2 errors) ====
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts (1 errors) ====

export interface Foo {
a: string;
Expand All @@ -16,9 +14,6 @@ tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredic
~~~~
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface.
let dis = this as Foo;
~~~~~~~~~~~
!!! error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
!!! error TS2352: Property 'a' is missing in type '{ m(): this is Foo; }'.
return dis.a != null && dis.b != null && dis.c != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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(10,19): error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
Property 'a' is missing in type '{ m(): this is Foo; }'.


==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts (3 errors) ====
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts (2 errors) ====

interface Foo {
a: string;
Expand All @@ -19,9 +17,6 @@ tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredic
~~~~
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface.
let dis = this as Foo;
~~~~~~~~~~~
!!! error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
!!! error TS2352: Property 'a' is missing in type '{ m(): this is Foo; }'.
return dis.a != null && dis.b != null && dis.c != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ var object = {

get 0() {
return this._0;
>this._0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
>this : Symbol(, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 12))
>_0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))

},
set 0(x: number) {
>x : Symbol(x, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 6, 10))

this._0 = x;
>this._0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
>this : Symbol(, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 12))
>_0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
>x : Symbol(x, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 6, 10))

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ var object = {

get 0() {
return this._0;
>this._0 : number
>this : { 0: number; _0: number; }
>_0 : number
>this._0 : any
>this : any
>_0 : any

},
set 0(x: number) {
>x : number

this._0 = x;
>this._0 = x : number
>this._0 : number
>this : { 0: number; _0: number; }
>_0 : number
>this._0 : any
>this : any
>_0 : any
>x : number

},
Expand Down
5 changes: 0 additions & 5 deletions tests/baselines/reference/fatarrowfunctions.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ var messenger = {

setTimeout(() => { this.message.toString(); }, 3000);
>setTimeout : Symbol(setTimeout, Decl(fatarrowfunctions.ts, 34, 1))
>this.message.toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
>this.message : Symbol(message, Decl(fatarrowfunctions.ts, 38, 17))
>this : Symbol(, Decl(fatarrowfunctions.ts, 38, 15))
>message : Symbol(message, Decl(fatarrowfunctions.ts, 38, 17))
>toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
}
};

12 changes: 6 additions & 6 deletions tests/baselines/reference/fatarrowfunctions.types
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ var messenger = {
>setTimeout(() => { this.message.toString(); }, 3000) : number
>setTimeout : (expression: any, msec?: number, language?: any) => number
>() => { this.message.toString(); } : () => void
>this.message.toString() : string
>this.message.toString : () => string
>this.message : string
>this : { message: string; start: () => void; }
>message : string
>toString : () => string
>this.message.toString() : any
>this.message.toString : any
>this.message : any
>this : any
>message : any
>toString : any
>3000 : number
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ var messenger = {

var _self = this;
>_self : Symbol(_self, Decl(fatarrowfunctionsInFunctions.ts, 5, 11))
>this : Symbol(, Decl(fatarrowfunctionsInFunctions.ts, 2, 15))

setTimeout(function() {
>setTimeout : Symbol(setTimeout, Decl(fatarrowfunctionsInFunctions.ts, 0, 0))

_self.message.toString();
>_self.message.toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
>_self.message : Symbol(message, Decl(fatarrowfunctionsInFunctions.ts, 2, 17))
>_self : Symbol(_self, Decl(fatarrowfunctionsInFunctions.ts, 5, 11))
>message : Symbol(message, Decl(fatarrowfunctionsInFunctions.ts, 2, 17))
>toString : Symbol(String.toString, Decl(lib.d.ts, --, --))

}, 3000);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/fatarrowfunctionsInFunctions.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ var messenger = {
>function() { var _self = this; setTimeout(function() { _self.message.toString(); }, 3000); } : () => void

var _self = this;
>_self : { message: string; start: () => void; }
>this : { message: string; start: () => void; }
>_self : any
>this : any

setTimeout(function() {
>setTimeout(function() { _self.message.toString(); }, 3000) : number
>setTimeout : (expression: any, msec?: number, language?: any) => number
>function() { _self.message.toString(); } : () => void

_self.message.toString();
>_self.message.toString() : string
>_self.message.toString : () => string
>_self.message : string
>_self : { message: string; start: () => void; }
>message : string
>toString : () => string
>_self.message.toString() : any
>_self.message.toString : any
>_self.message : any
>_self : any
>message : any
>toString : any

}, 3000);
>3000 : number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(21,1): error TS2322: Type '(this: C, m: number) => number' is not assignable to type '(this: void, m: number) => number'.
The 'this' types of each signature are incompatible.
Type 'void' is not assignable to type 'C'.
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(25,27): error TS2339: Property 'length' does not exist on type 'number'.
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(33,28): error TS2339: Property 'length' does not exist on type 'number'.
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(37,9): error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'I'.
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error TS2339: Property 'length' does not exist on type 'number'.


==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (5 errors) ====
==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (4 errors) ====
interface I {
n: number;
explicitThis(this: this, m: number): number;
Expand Down Expand Up @@ -37,8 +36,6 @@ tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error
n: 101,
explicitThis: function (m: number) {
return m + this.n.length; // error, 'length' does not exist on 'number'
~~~~~~
!!! error TS2339: Property 'length' does not exist on type 'number'.
},
implicitThis(m: number): number { return m; }
};
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/noImplicitThisObjectLiterals.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tests/cases/compiler/noImplicitThisObjectLiterals.ts(2,8): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
tests/cases/compiler/noImplicitThisObjectLiterals.ts(4,16): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
tests/cases/compiler/noImplicitThisObjectLiterals.ts(7,16): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.


==== tests/cases/compiler/noImplicitThisObjectLiterals.ts (3 errors) ====
let o = {
d: this, // error, this: any
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
m() {
return this.d.length; // error, this: any
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
},
f: function() {
return this.d.length; // error, this: any
~~~~
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
}
}

22 changes: 22 additions & 0 deletions tests/baselines/reference/noImplicitThisObjectLiterals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [noImplicitThisObjectLiterals.ts]
let o = {
d: this, // error, this: any
m() {
return this.d.length; // error, this: any
},
f: function() {
return this.d.length; // error, this: any
}
}


//// [noImplicitThisObjectLiterals.js]
var o = {
d: this,
m: function () {
return this.d.length; // error, this: any
},
f: function () {
return this.d.length; // error, this: any
}
};
7 changes: 0 additions & 7 deletions tests/baselines/reference/selfInLambdas.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ var o = {
>onmousemove : Symbol(Window.onmousemove, Decl(selfInLambdas.ts, 6, 18))

this.counter++
>this.counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
>this : Symbol(, Decl(selfInLambdas.ts, 10, 7))
>counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))

var f = () => this.counter;
>f : Symbol(f, Decl(selfInLambdas.ts, 18, 15))
>this.counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
>this : Symbol(, Decl(selfInLambdas.ts, 10, 7))
>counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))

}

Expand Down
Loading

0 comments on commit d1b60f8

Please sign in to comment.