You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Functions are compatible with ArrayLike<any> #18757
Essentially, this issue is a question: how are index signatures used when determining compatibility?
With functions, the index signature appears to be considered only if the signature's value type is not any:
// OK:consta=()=>{};constb: {[n: number]: any}=a;// ERROR: Index signature is missing in type '() => void'.constc=()=>{};constd: {[n: number]: string}=c;
However, with interfaces, the index signature appears to be considered only if it is present:
Searching has not yielded any answer as to what the expected behaviour is. And the considering of functions to be compatible with ArrayLike<any> is problematic with RxJS, as it results in projection functions being considered compatible with ObservableInput<any>.
The real culprit is the length property. ArrayLike specifies an index signature and a length; all functions do have a length property. The index signature doesn't match, except when the target index signature's type is any. This a somewhat ad-hoc rule based on the observation that { [s: string]: any } is effectively indistinguishable from the any type in TypeScript, so all types can match against that constraint without having an explicitly declared indexer.
We can explore backing out the special rule about any-targeted index signatures to only apply to string index signatures rather than number index signatures since the latter seems like an oversight at this point. But it'll be a breaking change and we'll have to make sure there aren't unintended side effects
Nathan Shively-Sanders (@sandersn) we'd like to see what happens in RWC if we restrict the "any signature means whatever" behavior to string signatures only
Activity
cartant commentedon Sep 27, 2017
Essentially, this issue is a question: how are index signatures used when determining compatibility?
With functions, the index signature appears to be considered only if the signature's value type is not
any:However, with interfaces, the index signature appears to be considered only if it is present:
Searching has not yielded any answer as to what the expected behaviour is. And the considering of functions to be compatible with
ArrayLike<any>is problematic with RxJS, as it results in projection functions being considered compatible withObservableInput<any>.RyanCavanaugh commentedon Sep 27, 2017
The real culprit is the
lengthproperty. ArrayLike specifies an index signature and a length; all functions do have alengthproperty. The index signature doesn't match, except when the target index signature's type isany. This a somewhat ad-hoc rule based on the observation that{ [s: string]: any }is effectively indistinguishable from theanytype in TypeScript, so all types can match against that constraint without having an explicitly declared indexer.We can explore backing out the special rule about
any-targeted index signatures to only apply to string index signatures rather than number index signatures since the latter seems like an oversight at this point. But it'll be a breaking change and we'll have to make sure there aren't unintended side effectscartant commentedon Sep 27, 2017
Thanks. I'd guessed that there's an ad-hoc rule regarding
{ [s: string]: any }in there somewhere.I'm curious as to why the seemingly-similar interface scenario does not effect an error:
Is there an explanation for what appears to be differing behaviour?
RyanCavanaugh commentedon Oct 10, 2017
Nathan Shively-Sanders (@sandersn) we'd like to see what happens in RWC if we restrict the "
anysignature means whatever" behavior to string signatures onlybenlesh commentedon Feb 10, 2021
Any progress on this? We're still hitting it