Skip to content

Call back into getTypeOfFuncClassEnumModule in getTypeOfVariableOrParameterOrProperty #20939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8 changes: 6 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4685,7 +4685,11 @@ namespace ts {
|| isIdentifier(declaration)
|| (isMethodDeclaration(declaration) && !isObjectLiteralMethod(declaration))
|| isMethodSignature(declaration)) {
// TODO: Mimics old behavior from incorrect usage of getWidenedTypeForVariableLikeDeclaration, but seems incorrect

// Symbol is property of some kind that is merged with something - should use `getTypeOfFuncClassEnumModule` and not `getTypeOfVariableOrParameterOrProperty`
if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
return getTypeOfFuncClassEnumModule(symbol);
}
type = tryGetTypeFromEffectiveTypeNode(declaration) || anyType;
}
else if (isPropertyAssignment(declaration)) {
Expand Down Expand Up @@ -21644,7 +21648,7 @@ namespace ts {
return;
}
const symbol = getSymbolOfNode(node);
const type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol));
const type = convertAutoToAny(getTypeOfSymbol(symbol));
if (node === symbol.valueDeclaration) {
// Node is the primary declaration of the symbol, just validate the initializer
// Don't validate for-in initializer as it is already an error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
tests/cases/compiler/classWithDuplicateIdentifier.ts(3,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(3,5): error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type '() => number', but here has type 'number'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(6,5): error TS2300: Duplicate identifier 'b'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(7,5): error TS2300: Duplicate identifier 'b'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2300: Duplicate identifier 'c'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2717: Subsequent property declarations must have the same type. Property 'c' must be of type 'number', but here has type 'string'.


==== tests/cases/compiler/classWithDuplicateIdentifier.ts (5 errors) ====
==== tests/cases/compiler/classWithDuplicateIdentifier.ts (6 errors) ====
class C {
a(): number { return 0; } // error: duplicate identifier
a: number;
~
!!! error TS2300: Duplicate identifier 'a'.
~
!!! error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type '() => number', but here has type 'number'.
}
class K {
b: number; // error: duplicate identifier
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/classWithDuplicateIdentifier.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class C {
>C : C

a(): number { return 0; } // error: duplicate identifier
>a : number
>a : () => number
>0 : 0

a: number;
>a : number
>a : () => number
}
class K {
>K : K
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/methodSignatureHandledDeclarationKindForSymbol.ts(6,5): error TS2717: Subsequent property declarations must have the same type. Property 'bold' must be of type '() => string', but here has type 'string'.


==== tests/cases/compiler/methodSignatureHandledDeclarationKindForSymbol.ts (1 errors) ====
interface Foo {
bold(): string;
}

interface Foo {
bold: string;
~~~~
!!! error TS2717: Subsequent property declarations must have the same type. Property 'bold' must be of type '() => string', but here has type 'string'.
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ interface Foo {
>Foo : Foo

bold(): string;
>bold : string
>bold : () => string
}

interface Foo {
>Foo : Foo

bold: string;
>bold : string
>bold : () => string
}


30 changes: 15 additions & 15 deletions tests/baselines/reference/multipleDeclarations.types
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ class X {

this.mistake = 'frankly, complete nonsense';
>this.mistake = 'frankly, complete nonsense' : "frankly, complete nonsense"
>this.mistake : any
>this.mistake : () => void
>this : this
>mistake : any
>mistake : () => void
>'frankly, complete nonsense' : "frankly, complete nonsense"
}
m() {
>m : () => void
}
mistake() {
>mistake : any
>mistake : () => void
}
}
let x = new X();
Expand All @@ -62,11 +62,11 @@ let x = new X();

X.prototype.mistake = false;
>X.prototype.mistake = false : false
>X.prototype.mistake : any
>X.prototype.mistake : () => void
>X.prototype : X
>X : typeof X
>prototype : X
>mistake : any
>mistake : () => void
>false : false

x.m();
Expand All @@ -76,15 +76,15 @@ x.m();
>m : () => void

x.mistake;
>x.mistake : any
>x.mistake : () => void
>x : X
>mistake : any
>mistake : () => void

class Y {
>Y : Y

mistake() {
>mistake : any
>mistake : () => void
}
m() {
>m : () => void
Expand All @@ -105,19 +105,19 @@ class Y {

this.mistake = 'even more nonsense';
>this.mistake = 'even more nonsense' : "even more nonsense"
>this.mistake : any
>this.mistake : () => void
>this : this
>mistake : any
>mistake : () => void
>'even more nonsense' : "even more nonsense"
}
}
Y.prototype.mistake = true;
>Y.prototype.mistake = true : true
>Y.prototype.mistake : any
>Y.prototype.mistake : () => void
>Y.prototype : Y
>Y : typeof Y
>prototype : Y
>mistake : any
>mistake : () => void
>true : true

let y = new Y();
Expand All @@ -132,8 +132,8 @@ y.m();
>m : () => void

y.mistake();
>y.mistake() : any
>y.mistake : any
>y.mistake() : void
>y.mistake : () => void
>y : Y
>mistake : any
>mistake : () => void

Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
=== tests/cases/conformance/salsa/index.js ===
Common.Item = class I {}
>Common.Item : Symbol(Common.Item, Decl(index.js, 0, 0))
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
>Item : Symbol(Common.Item, Decl(index.js, 0, 0))
>I : Symbol(I, Decl(index.js, 0, 13))

Common.Object = class extends Common.Item {}
>Common.Object : Symbol(Common.Object, Decl(index.js, 0, 24))
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
>Object : Symbol(Common.Object, Decl(index.js, 0, 24))
>Common.Item : Symbol(Common.Item, Decl(index.js, 0, 0))
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
>Item : Symbol(Common.Item, Decl(index.js, 0, 0))

Workspace.Object = class extends Common.Object {}
>Workspace.Object : Symbol(Workspace.Object, Decl(index.js, 1, 44))
>Workspace : Symbol(Workspace, Decl(index.js, 1, 44), Decl(roots.js, 1, 3))
>Object : Symbol(Workspace.Object, Decl(index.js, 1, 44))
>Common.Object : Symbol(Common.Object, Decl(index.js, 0, 24))
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
>Object : Symbol(Common.Object, Decl(index.js, 0, 24))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
=== tests/cases/conformance/salsa/index.js ===
Common.Item = class I {}
>Common.Item = class I {} : typeof I
>Common.Item : any
>Common : any
>Item : any
>Common.Item : typeof I
>Common : typeof Common
>Item : typeof I
>class I {} : typeof I
>I : typeof I

Common.Object = class extends Common.Item {}
>Common.Object = class extends Common.Item {} : typeof (Anonymous class)
>Common.Object : any
>Common : any
>Object : any
>Common.Object : typeof (Anonymous class)
>Common : typeof Common
>Object : typeof (Anonymous class)
>class extends Common.Item {} : typeof (Anonymous class)
>Common.Item : any
>Common : any
>Item : any
>Common.Item : I
>Common : typeof Common
>Item : typeof I

Workspace.Object = class extends Common.Object {}
>Workspace.Object = class extends Common.Object {} : typeof (Anonymous class)
>Workspace.Object : any
>Workspace : any
>Object : any
>Workspace.Object : typeof (Anonymous class)
>Workspace : typeof Workspace
>Object : typeof (Anonymous class)
>class extends Common.Object {} : typeof (Anonymous class)
>Common.Object : any
>Common : any
>Object : any
>Common.Object : (Anonymous class)
>Common : typeof Common
>Object : typeof (Anonymous class)

/** @type {Workspace.Object} */
var am;
>am : (Anonymous class)

=== tests/cases/conformance/salsa/roots.js ===
var Common = {};
>Common : any
>Common : typeof Common
>{} : { [x: string]: any; }

var Workspace = {};
>Workspace : any
>Workspace : typeof Workspace
>{} : { [x: string]: any; }