Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6574,8 +6574,8 @@ namespace ts {

// Return contextual type of parameter or undefined if no contextual type is available
function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type {
if (isFunctionExpressionOrArrowFunction(parameter.parent)) {
let func = <FunctionExpression>parameter.parent;
let func = parameter.parent;
if (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) {
if (isContextSensitive(func)) {
let contextualSignature = getContextualSignature(func);
if (contextualSignature) {
Expand Down Expand Up @@ -6907,7 +6907,7 @@ namespace ts {
}
}

function isFunctionExpressionOrArrowFunction(node: Node): boolean {
function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression {
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
}

Expand All @@ -6926,8 +6926,8 @@ namespace ts {
function getContextualSignature(node: FunctionExpression | MethodDeclaration): Signature {
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
let type = isObjectLiteralMethod(node)
? getContextualTypeForObjectLiteralMethod(<MethodDeclaration>node)
: getContextualType(<FunctionExpression>node);
? getContextualTypeForObjectLiteralMethod(node)
: getContextualType(node);
if (!type) {
return undefined;
}
Expand Down Expand Up @@ -13656,7 +13656,7 @@ namespace ts {
forEach(node.decorators, checkFunctionAndClassExpressionBodies);
forEach((<MethodDeclaration>node).parameters, checkFunctionAndClassExpressionBodies);
if (isObjectLiteralMethod(node)) {
checkFunctionExpressionOrObjectLiteralMethodBody(<MethodDeclaration>node);
checkFunctionExpressionOrObjectLiteralMethodBody(node);
}
break;
case SyntaxKind.Constructor:
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ namespace ts {
return node && node.kind === SyntaxKind.Block && isFunctionLike(node.parent);
}

export function isObjectLiteralMethod(node: Node) {
export function isObjectLiteralMethod(node: Node): node is MethodDeclaration {
return node && node.kind === SyntaxKind.MethodDeclaration && node.parent.kind === SyntaxKind.ObjectLiteralExpression;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(17,24): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(20,24): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(28,27): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(31,27): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(39,36): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts(42,36): error TS7006: Parameter 'arg' implicitly has an 'any' type.


==== tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration01.ts (6 errors) ====

interface A {
numProp: number;
}

interface B {
strProp: string;
}

interface Foo {
method1(arg: A): void;
method2(arg: B): void;
}

function getFoo1(): Foo {
return class {
static method1(arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
static method2(arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}

function getFoo2(): Foo {
return class {
static method1 = (arg) => {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
static method2 = (arg) => {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}

function getFoo3(): Foo {
return class {
static method1 = function (arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
static method2 = function (arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//// [contextuallyTypedClassExpressionMethodDeclaration01.ts]

interface A {
numProp: number;
}

interface B {
strProp: string;
}

interface Foo {
method1(arg: A): void;
method2(arg: B): void;
}

function getFoo1(): Foo {
return class {
static method1(arg) {
arg.numProp = 10;
}
static method2(arg) {
arg.strProp = "hello";
}
}
}

function getFoo2(): Foo {
return class {
static method1 = (arg) => {
arg.numProp = 10;
}
static method2 = (arg) => {
arg.strProp = "hello";
}
}
}

function getFoo3(): Foo {
return class {
static method1 = function (arg) {
arg.numProp = 10;
}
static method2 = function (arg) {
arg.strProp = "hello";
}
}
}

//// [contextuallyTypedClassExpressionMethodDeclaration01.js]
function getFoo1() {
return (function () {
function class_1() {
}
class_1.method1 = function (arg) {
arg.numProp = 10;
};
class_1.method2 = function (arg) {
arg.strProp = "hello";
};
return class_1;
})();
}
function getFoo2() {
return (function () {
function class_2() {
}
class_2.method1 = function (arg) {
arg.numProp = 10;
};
class_2.method2 = function (arg) {
arg.strProp = "hello";
};
return class_2;
})();
}
function getFoo3() {
return (function () {
function class_3() {
}
class_3.method1 = function (arg) {
arg.numProp = 10;
};
class_3.method2 = function (arg) {
arg.strProp = "hello";
};
return class_3;
})();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(21,17): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(24,17): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(32,20): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(35,20): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(43,29): error TS7006: Parameter 'arg' implicitly has an 'any' type.
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts(46,29): error TS7006: Parameter 'arg' implicitly has an 'any' type.


==== tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedClassExpressionMethodDeclaration02.ts (6 errors) ====

interface A {
numProp: number;
}

interface B {
strProp: string;
}

interface Foo {
new (): Bar;
}

interface Bar {
method1(arg: A): void;
method2(arg: B): void;
}

function getFoo1(): Foo {
return class {
method1(arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
method2(arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}

function getFoo2(): Foo {
return class {
method1 = (arg) => {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
method2 = (arg) => {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}

function getFoo3(): Foo {
return class {
method1 = function (arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.numProp = 10;
}
method2 = function (arg) {
~~~
!!! error TS7006: Parameter 'arg' implicitly has an 'any' type.
arg.strProp = "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//// [contextuallyTypedClassExpressionMethodDeclaration02.ts]

interface A {
numProp: number;
}

interface B {
strProp: string;
}

interface Foo {
new (): Bar;
}

interface Bar {
method1(arg: A): void;
method2(arg: B): void;
}

function getFoo1(): Foo {
return class {
method1(arg) {
arg.numProp = 10;
}
method2(arg) {
arg.strProp = "hello";
}
}
}

function getFoo2(): Foo {
return class {
method1 = (arg) => {
arg.numProp = 10;
}
method2 = (arg) => {
arg.strProp = "hello";
}
}
}

function getFoo3(): Foo {
return class {
method1 = function (arg) {
arg.numProp = 10;
}
method2 = function (arg) {
arg.strProp = "hello";
}
}
}

//// [contextuallyTypedClassExpressionMethodDeclaration02.js]
function getFoo1() {
return (function () {
function class_1() {
}
class_1.prototype.method1 = function (arg) {
arg.numProp = 10;
};
class_1.prototype.method2 = function (arg) {
arg.strProp = "hello";
};
return class_1;
})();
}
function getFoo2() {
return (function () {
function class_2() {
this.method1 = function (arg) {
arg.numProp = 10;
};
this.method2 = function (arg) {
arg.strProp = "hello";
};
}
return class_2;
})();
}
function getFoo3() {
return (function () {
function class_3() {
this.method1 = function (arg) {
arg.numProp = 10;
};
this.method2 = function (arg) {
arg.strProp = "hello";
};
}
return class_3;
})();
}
Loading