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
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
6 changes: 5 additions & 1 deletion internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return TernaryTrue
}
if target == r.c.anyFunctionType {
return TernaryFalse
}
sourceSignatures := r.c.getSignaturesOfType(source, kind)
targetSignatures := r.c.getSignaturesOfType(target, kind)
if kind == SignatureKindConstruct && len(sourceSignatures) != 0 && len(targetSignatures) != 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
subtypeReductionWithAnyFunctionType.ts(10,16): error TS7006: Parameter 'x' implicitly has an 'any' type.


==== subtypeReductionWithAnyFunctionType.ts (1 errors) ====
// https://github.com/microsoft/typescript-go/issues/849

declare function useMemo<T>(func: () => T): T;

function getPredicate(alwaysTrue: boolean) {
const predicate: (input: string) => boolean = useMemo(() => {
if (alwaysTrue) {
return () => true;
}
return x => x.length > 0;
~
!!! error TS7006: Parameter 'x' implicitly has an 'any' type.
});
return predicate;
}

// https://github.com/microsoft/typescript-go/issues/1016

declare function compact<T>(array: T[]): T[];
declare function makeFooer(): Fooer;
interface Fooer {
foo: (v: string) => string;
}
function f() {
const _ = compact([makeFooer(), { foo: (v) => v }]);
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,42 @@ function getPredicate(alwaysTrue: boolean) {
}
return x => x.length > 0;
>x : Symbol(x, Decl(subtypeReductionWithAnyFunctionType.ts, 9, 14))
>x.length : Symbol(length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(subtypeReductionWithAnyFunctionType.ts, 9, 14))
>length : Symbol(length, Decl(lib.es5.d.ts, --, --))

});
return predicate;
>predicate : Symbol(predicate, Decl(subtypeReductionWithAnyFunctionType.ts, 5, 9))
}

// https://github.com/microsoft/typescript-go/issues/1016

declare function compact<T>(array: T[]): T[];
>compact : Symbol(compact, Decl(subtypeReductionWithAnyFunctionType.ts, 12, 1))
>T : Symbol(T, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 25))
>array : Symbol(array, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 28))
>T : Symbol(T, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 25))
>T : Symbol(T, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 25))

declare function makeFooer(): Fooer;
>makeFooer : Symbol(makeFooer, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 45))
>Fooer : Symbol(Fooer, Decl(subtypeReductionWithAnyFunctionType.ts, 17, 36))

interface Fooer {
>Fooer : Symbol(Fooer, Decl(subtypeReductionWithAnyFunctionType.ts, 17, 36))

foo: (v: string) => string;
>foo : Symbol(foo, Decl(subtypeReductionWithAnyFunctionType.ts, 18, 17))
>v : Symbol(v, Decl(subtypeReductionWithAnyFunctionType.ts, 19, 10))
}
function f() {
>f : Symbol(f, Decl(subtypeReductionWithAnyFunctionType.ts, 20, 1))

const _ = compact([makeFooer(), { foo: (v) => v }]);
>_ : Symbol(_, Decl(subtypeReductionWithAnyFunctionType.ts, 22, 9))
>compact : Symbol(compact, Decl(subtypeReductionWithAnyFunctionType.ts, 12, 1))
>makeFooer : Symbol(makeFooer, Decl(subtypeReductionWithAnyFunctionType.ts, 16, 45))
>foo : Symbol(foo, Decl(subtypeReductionWithAnyFunctionType.ts, 22, 37))
>v : Symbol(v, Decl(subtypeReductionWithAnyFunctionType.ts, 22, 44))
>v : Symbol(v, Decl(subtypeReductionWithAnyFunctionType.ts, 22, 44))
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function getPredicate(alwaysTrue: boolean) {
const predicate: (input: string) => boolean = useMemo(() => {
>predicate : (input: string) => boolean
>input : string
>useMemo(() => { if (alwaysTrue) { return () => true; } return x => x.length > 0; }) : (x: string) => boolean
>useMemo(() => { if (alwaysTrue) { return () => true; } return x => x.length > 0; }) : (x: any) => boolean
>useMemo : <T>(func: () => T) => T
>() => { if (alwaysTrue) { return () => true; } return x => x.length > 0; } : () => (x: string) => boolean
>() => { if (alwaysTrue) { return () => true; } return x => x.length > 0; } : () => (x: any) => boolean

if (alwaysTrue) {
>alwaysTrue : boolean
Expand All @@ -26,16 +26,47 @@ function getPredicate(alwaysTrue: boolean) {
>true : true
}
return x => x.length > 0;
>x => x.length > 0 : (x: string) => boolean
>x : string
>x => x.length > 0 : (x: any) => boolean
>x : any
>x.length > 0 : boolean
>x.length : number
>x : string
>length : number
>x.length : any
>x : any
>length : any
>0 : 0

});
return predicate;
>predicate : (input: string) => boolean
}

// https://github.com/microsoft/typescript-go/issues/1016

declare function compact<T>(array: T[]): T[];
>compact : <T>(array: T[]) => T[]
>array : T[]

declare function makeFooer(): Fooer;
>makeFooer : () => Fooer

interface Fooer {
foo: (v: string) => string;
>foo : (v: string) => string
>v : string
}
function f() {
>f : () => void

const _ = compact([makeFooer(), { foo: (v) => v }]);
>_ : Fooer[]
>compact([makeFooer(), { foo: (v) => v }]) : Fooer[]
>compact : <T>(array: T[]) => T[]
>[makeFooer(), { foo: (v) => v }] : Fooer[]
>makeFooer() : Fooer
>makeFooer : () => Fooer
>{ foo: (v) => v } : { foo: (v: string) => string; }
>foo : (v: string) => string
>(v) => v : (v: string) => string
>v : string
>v : string
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ function getPredicate(alwaysTrue: boolean) {
});
return predicate;
}

// https://github.com/microsoft/typescript-go/issues/1016

declare function compact<T>(array: T[]): T[];
declare function makeFooer(): Fooer;
interface Fooer {
foo: (v: string) => string;
}
function f() {
const _ = compact([makeFooer(), { foo: (v) => v }]);
}