Skip to content

Make anyFunctionType a subtype of all function types #1149

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 3 commits into from
Jun 10, 2025
Merged

Conversation

ahejlsberg
Copy link
Member

Implements what I discuss here.

Fixes #1016.

@Copilot Copilot AI review requested due to automatic review settings June 10, 2025 13:11
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Introduces anyFunctionType as a wildcard subtype of all other function types, updating signature relation logic and test baselines.

  • Adds early-return logic in signaturesRelatedTo to treat anyFunctionType as a subtype (but not a supertype) of functions.
  • Updates test baselines to reflect that callbacks inferred from useMemo now have parameter type any and trigger an implicit-any error.
  • Adjusts symbol and error snapshots to expect any for parameter x.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
internal/checker/relater.go Added checks to return TernaryTrue/TernaryFalse for anyFunctionType subtyping
testdata/baselines/reference/compiler/subtypeReductionWithAnyFunctionType.types Updated expected type prints to (x: any) => boolean
testdata/baselines/reference/compiler/subtypeReductionWithAnyFunctionType.symbols Adjusted symbol entries so x is declared with type any
testdata/baselines/reference/compiler/subtypeReductionWithAnyFunctionType.errors.txt Now reports a TS7006 implicit-any error for parameter x

@@ -4359,9 +4359,13 @@ func (r *Relater) signaturesRelatedTo(source *Type, target *Type, kind Signature
if r.relation == r.c.identityRelation {
return r.signaturesIdenticalTo(source, target, kind)
}
if target == r.c.anyFunctionType || r.relation != r.c.strictSubtypeRelation && source == r.c.anyFunctionType {
// With respect to signatures, the anyFunctionType wildcard is a subtype of every other function type.
if source == r.c.anyFunctionType {
Copy link
Preview

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

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

The unconditional TernaryTrue when source is anyFunctionType bypasses signature checks (including return types), which could incorrectly mark incompatible function signatures as subtypes. Consider preserving return-type compatibility or narrowing the early return condition.

Suggested change
if source == r.c.anyFunctionType {
if source == r.c.anyFunctionType {
sourceSignatures := r.c.getSignaturesOfType(source, kind)
targetSignatures := r.c.getSignaturesOfType(target, kind)
for _, s := range sourceSignatures {
for _, t := range targetSignatures {
if r.signatureRelatedTo(s, t, true /*erase*/, reportErrors, intersectionState) == TernaryFalse {
return TernaryFalse
}
}
}

Copilot uses AI. Check for mistakes.

@jakebailey jakebailey added this pull request to the merge queue Jun 10, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 10, 2025
@jakebailey jakebailey added this pull request to the merge queue Jun 10, 2025
Merged via the queue into main with commit d9b5a75 Jun 10, 2025
23 checks passed
@jakebailey jakebailey deleted the fix-1016 branch June 10, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancy inferring function parameter from context
2 participants