Skip to content

Contextual type of methods only in noImplicitAny or JS #50542

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9134,6 +9134,26 @@ namespace ts {
if (type) {
return addOptionality(type, /*isProperty*/ false, isOptional);
}
if (isMethodDeclaration(func) && isClassLike(func.parent)
&& (noImplicitAny || isInJSFile(func.parent))) { // TODO: Might want to fiddle with restrictions here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity - what's the reasoning to only apply this logic with noImplicitAny (and in JS files)? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. noImplicitAny: untyped parameters will get an error at the same time they get any, so this change can't cause code with 0 errors to compile with >0 errors.
  2. JS files: in JS, errors aren't reported anyway, so any improvement in typing should show up as improvement in completions. (this might need to change to exclude checkjs)

const k = func.parent;
const basetypes = getBaseTypes(getTypeOfSymbol(getSymbolOfNode(k)) as InterfaceType); // TODO: Assumption of prototype
if (basetypes.length === 1 && isIdentifier(func.name)) { // TODO: Limitations of prototype
const methodType = getTypeOfPropertyOfType(basetypes[0], func.name.escapedText)
if (methodType) {
const sig = getSingleCallSignature(methodType)
if (sig) {
const i = func.parameters.indexOf(declaration)
if (i > -1) {
const t = tryGetTypeAtPosition(sig, i)
if (t) { // wtb nullable monad
return addOptionality(t, /*isProperty*/ false, isOptional);
}
}
}
}
}
}
}

// Use the type of the initializer expression if one is present and the declaration is
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/quickInfoJsDocTags5.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"kind": "space"
},
{
"text": "any",
"text": "number",
"kind": "keyword"
},
{
Expand All @@ -82,7 +82,7 @@
"kind": "space"
},
{
"text": "any",
"text": "number",
"kind": "keyword"
},
{
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/quickInfoJsDocTags6.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"kind": "space"
},
{
"text": "any",
"text": "number",
"kind": "keyword"
},
{
Expand All @@ -82,7 +82,7 @@
"kind": "space"
},
{
"text": "any",
"text": "number",
"kind": "keyword"
},
{
Expand Down