Skip to content

Commit 04c2fbd

Browse files
committed
Contextual type of methods only in noImplicitAny or JSDoc
1 parent 488d0ee commit 04c2fbd

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9134,6 +9134,26 @@ namespace ts {
91349134
if (type) {
91359135
return addOptionality(type, /*isProperty*/ false, isOptional);
91369136
}
9137+
if (isMethodDeclaration(func) && isClassLike(func.parent)
9138+
&& (noImplicitAny || isInJSFile(func.parent))) { // TODO: Might want to fiddle with restrictions here
9139+
const k = func.parent;
9140+
const basetypes = getBaseTypes(getTypeOfSymbol(getSymbolOfNode(k)) as InterfaceType); // TODO: Assumption of prototype
9141+
if (basetypes.length === 1 && isIdentifier(func.name)) { // TODO: Limitations of prototype
9142+
const methodType = getTypeOfPropertyOfType(basetypes[0], func.name.escapedText)
9143+
if (methodType) {
9144+
const sig = getSingleCallSignature(methodType)
9145+
if (sig) {
9146+
const i = func.parameters.indexOf(declaration)
9147+
if (i > -1) {
9148+
const t = tryGetTypeAtPosition(sig, i)
9149+
if (t) { // wtb nullable monad
9150+
return addOptionality(t, /*isProperty*/ false, isOptional);
9151+
}
9152+
}
9153+
}
9154+
}
9155+
}
9156+
}
91379157
}
91389158

91399159
// Use the type of the initializer expression if one is present and the declaration is

tests/baselines/reference/quickInfoJsDocTags5.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"kind": "space"
5959
},
6060
{
61-
"text": "any",
61+
"text": "number",
6262
"kind": "keyword"
6363
},
6464
{
@@ -82,7 +82,7 @@
8282
"kind": "space"
8383
},
8484
{
85-
"text": "any",
85+
"text": "number",
8686
"kind": "keyword"
8787
},
8888
{

tests/baselines/reference/quickInfoJsDocTags6.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"kind": "space"
5959
},
6060
{
61-
"text": "any",
61+
"text": "number",
6262
"kind": "keyword"
6363
},
6464
{
@@ -82,7 +82,7 @@
8282
"kind": "space"
8383
},
8484
{
85-
"text": "any",
85+
"text": "number",
8686
"kind": "keyword"
8787
},
8888
{

0 commit comments

Comments
 (0)