mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Check this expressions in object literal methods
Add a test and baseline
This commit is contained in:
parent
482acccada
commit
7b531fcd05
@ -7494,6 +7494,14 @@ namespace ts {
|
||||
const symbol = getSymbolOfNode(container.parent);
|
||||
return container.flags & NodeFlags.Static ? getTypeOfSymbol(symbol) : (<InterfaceType>getDeclaredTypeOfSymbol(symbol)).thisType;
|
||||
}
|
||||
if (container.parent && container.parent.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>container.parent);
|
||||
if (type) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
if (isInJavaScriptFile(node)) {
|
||||
const type = getTypeForThisExpressionFromJSDoc(container);
|
||||
|
||||
16
tests/baselines/reference/thisTypeInObjectLiterals.js
Normal file
16
tests/baselines/reference/thisTypeInObjectLiterals.js
Normal file
@ -0,0 +1,16 @@
|
||||
//// [thisTypeInObjectLiterals.ts]
|
||||
let o = {
|
||||
d: "bar",
|
||||
m() {
|
||||
return this.d.length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [thisTypeInObjectLiterals.js]
|
||||
var o = {
|
||||
d: "bar",
|
||||
m: function () {
|
||||
return this.d.length;
|
||||
}
|
||||
};
|
||||
19
tests/baselines/reference/thisTypeInObjectLiterals.symbols
Normal file
19
tests/baselines/reference/thisTypeInObjectLiterals.symbols
Normal file
@ -0,0 +1,19 @@
|
||||
=== tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts ===
|
||||
let o = {
|
||||
>o : Symbol(o, Decl(thisTypeInObjectLiterals.ts, 0, 3))
|
||||
|
||||
d: "bar",
|
||||
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
|
||||
|
||||
m() {
|
||||
>m : Symbol(m, Decl(thisTypeInObjectLiterals.ts, 1, 13))
|
||||
|
||||
return this.d.length;
|
||||
>this.d.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>this.d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
|
||||
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 0, 7))
|
||||
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
}
|
||||
}
|
||||
|
||||
21
tests/baselines/reference/thisTypeInObjectLiterals.types
Normal file
21
tests/baselines/reference/thisTypeInObjectLiterals.types
Normal file
@ -0,0 +1,21 @@
|
||||
=== tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts ===
|
||||
let o = {
|
||||
>o : { d: string; m(): number; }
|
||||
>{ d: "bar", m() { return this.d.length; }} : { d: string; m(): number; }
|
||||
|
||||
d: "bar",
|
||||
>d : string
|
||||
>"bar" : string
|
||||
|
||||
m() {
|
||||
>m : () => number
|
||||
|
||||
return this.d.length;
|
||||
>this.d.length : number
|
||||
>this.d : string
|
||||
>this : { d: string; m(): number; }
|
||||
>d : string
|
||||
>length : number
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
let o = {
|
||||
d: "bar",
|
||||
m() {
|
||||
return this.d.length;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user