diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cb26eb371b5..2b57aca48ac 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5503,7 +5503,7 @@ namespace ts { } } else if (source.flags & TypeFlags.ObjectType && (target.flags & (TypeFlags.Reference | TypeFlags.Tuple) || - (target.flags & TypeFlags.Anonymous) && target.symbol && target.symbol.flags & (SymbolFlags.Method | SymbolFlags.TypeLiteral))) { + (target.flags & TypeFlags.Anonymous) && target.symbol && target.symbol.flags & (SymbolFlags.Method | SymbolFlags.TypeLiteral | SymbolFlags.Class))) { // If source is an object type, and target is a type reference, a tuple type, the type of a method, or a type literal, infer from members if (isInProcess(source, target)) { return; diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.js b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.js new file mode 100644 index 00000000000..25ce3743fa2 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.js @@ -0,0 +1,22 @@ +//// [typeArgumentInferenceWithClassExpression1.ts] +function foo(x = class { static prop: T }): T { + return undefined; +} + +foo(class { static prop = "hello" }).length; + +//// [typeArgumentInferenceWithClassExpression1.js] +function foo(x) { + if (x === void 0) { x = (function () { + function class_1() { + } + return class_1; + })(); } + return undefined; +} +foo((function () { + function class_2() { + } + class_2.prop = "hello"; + return class_2; +})()).length; diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.symbols b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.symbols new file mode 100644 index 00000000000..ceb97a73af8 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts === +function foo(x = class { static prop: T }): T { +>foo : Symbol(foo, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 0)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 13)) +>x : Symbol(x, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 16)) +>prop : Symbol((Anonymous class).prop, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 27)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 13)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 13)) + + return undefined; +>undefined : Symbol(undefined) +} + +foo(class { static prop = "hello" }).length; +>foo(class { static prop = "hello" }).length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) +>foo : Symbol(foo, Decl(typeArgumentInferenceWithClassExpression1.ts, 0, 0)) +>prop : Symbol((Anonymous class).prop, Decl(typeArgumentInferenceWithClassExpression1.ts, 4, 11)) +>length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) + diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.types b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.types new file mode 100644 index 00000000000..63ee1309383 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression1.types @@ -0,0 +1,23 @@ +=== tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts === +function foo(x = class { static prop: T }): T { +>foo : (x?: typeof (Anonymous class)) => T +>T : T +>x : typeof (Anonymous class) +>class { static prop: T } : typeof (Anonymous class) +>prop : T +>T : T +>T : T + + return undefined; +>undefined : undefined +} + +foo(class { static prop = "hello" }).length; +>foo(class { static prop = "hello" }).length : number +>foo(class { static prop = "hello" }) : string +>foo : (x?: typeof (Anonymous class)) => T +>class { static prop = "hello" } : typeof (Anonymous class) +>prop : string +>"hello" : string +>length : number + diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.errors.txt b/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.errors.txt new file mode 100644 index 00000000000..c630944306b --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.errors.txt @@ -0,0 +1,16 @@ +tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression2.ts(6,5): error TS2345: Argument of type 'typeof (Anonymous class)' is not assignable to parameter of type 'typeof (Anonymous class)'. + Type '(Anonymous class)' is not assignable to type 'foo<{}>.'. + Property 'prop' is missing in type '(Anonymous class)'. + + +==== tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression2.ts (1 errors) ==== + function foo(x = class { prop: T }): T { + return undefined; + } + + // Should not infer string because it is a static property + foo(class { static prop = "hello" }).length; + ~~~~~ +!!! error TS2345: Argument of type 'typeof (Anonymous class)' is not assignable to parameter of type 'typeof (Anonymous class)'. +!!! error TS2345: Type '(Anonymous class)' is not assignable to type 'foo<{}>.'. +!!! error TS2345: Property 'prop' is missing in type '(Anonymous class)'. \ No newline at end of file diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.js b/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.js new file mode 100644 index 00000000000..c4a7872b016 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression2.js @@ -0,0 +1,24 @@ +//// [typeArgumentInferenceWithClassExpression2.ts] +function foo(x = class { prop: T }): T { + return undefined; +} + +// Should not infer string because it is a static property +foo(class { static prop = "hello" }).length; + +//// [typeArgumentInferenceWithClassExpression2.js] +function foo(x) { + if (x === void 0) { x = (function () { + function class_1() { + } + return class_1; + })(); } + return undefined; +} +// Should not infer string because it is a static property +foo((function () { + function class_2() { + } + class_2.prop = "hello"; + return class_2; +})()).length; diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.js b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.js new file mode 100644 index 00000000000..f3a3470e1c3 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.js @@ -0,0 +1,22 @@ +//// [typeArgumentInferenceWithClassExpression3.ts] +function foo(x = class { prop: T }): T { + return undefined; +} + +foo(class { prop = "hello" }).length; + +//// [typeArgumentInferenceWithClassExpression3.js] +function foo(x) { + if (x === void 0) { x = (function () { + function class_1() { + } + return class_1; + })(); } + return undefined; +} +foo((function () { + function class_2() { + this.prop = "hello"; + } + return class_2; +})()).length; diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.symbols b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.symbols new file mode 100644 index 00000000000..aedb0230fd6 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression3.ts === +function foo(x = class { prop: T }): T { +>foo : Symbol(foo, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 0)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 13)) +>x : Symbol(x, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 16)) +>prop : Symbol((Anonymous class).prop, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 27)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 13)) +>T : Symbol(T, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 13)) + + return undefined; +>undefined : Symbol(undefined) +} + +foo(class { prop = "hello" }).length; +>foo(class { prop = "hello" }).length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) +>foo : Symbol(foo, Decl(typeArgumentInferenceWithClassExpression3.ts, 0, 0)) +>prop : Symbol((Anonymous class).prop, Decl(typeArgumentInferenceWithClassExpression3.ts, 4, 11)) +>length : Symbol(String.length, Decl(lib.d.ts, 414, 19)) + diff --git a/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.types b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.types new file mode 100644 index 00000000000..9a2bddd9296 --- /dev/null +++ b/tests/baselines/reference/typeArgumentInferenceWithClassExpression3.types @@ -0,0 +1,23 @@ +=== tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression3.ts === +function foo(x = class { prop: T }): T { +>foo : (x?: typeof (Anonymous class)) => T +>T : T +>x : typeof (Anonymous class) +>class { prop: T } : typeof (Anonymous class) +>prop : T +>T : T +>T : T + + return undefined; +>undefined : undefined +} + +foo(class { prop = "hello" }).length; +>foo(class { prop = "hello" }).length : number +>foo(class { prop = "hello" }) : string +>foo : (x?: typeof (Anonymous class)) => T +>class { prop = "hello" } : typeof (Anonymous class) +>prop : string +>"hello" : string +>length : number + diff --git a/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts new file mode 100644 index 00000000000..21ca07ea2a2 --- /dev/null +++ b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts @@ -0,0 +1,5 @@ +function foo(x = class { static prop: T }): T { + return undefined; +} + +foo(class { static prop = "hello" }).length; \ No newline at end of file diff --git a/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression2.ts b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression2.ts new file mode 100644 index 00000000000..d7a901ae951 --- /dev/null +++ b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression2.ts @@ -0,0 +1,6 @@ +function foo(x = class { prop: T }): T { + return undefined; +} + +// Should not infer string because it is a static property +foo(class { static prop = "hello" }).length; \ No newline at end of file diff --git a/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression3.ts b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression3.ts new file mode 100644 index 00000000000..d29b7007664 --- /dev/null +++ b/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression3.ts @@ -0,0 +1,5 @@ +function foo(x = class { prop: T }): T { + return undefined; +} + +foo(class { prop = "hello" }).length; \ No newline at end of file