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
Function with optional destructured parameter not seen as compatible with its inferred type #37271
constfInferred=({ a =0}={})=>a;// const fInferred: ({ a }?: { a?: number; }) => numberconstfAnnotated: typeoffInferred=({ a =0}={})=>a;// error!// ----------------------------------> ~// 'a' does not exist on type '{ a?: number | undefined; } | undefined'constfAnnotationIntensifies: typeoffInferred=({ a =0}: {a?: number}={})=>a;// no error now
Expected behavior:
No error assigning to fAnnotated. The arrow function's parameter should be compatible with the contextual type, since this contextual type comes from the parameter type of fInferred with identical JS code.
Actual behavior:
The destructured parameter a of fAnnotated generates a warning. It is no longer inferred as an optional parameter. You can work around this by explicitly annotating the parameter as in fAnnotationIntensifies but it's not clear why this should be necessary.
This issue came up in this Stack Overflow question where the person wants to explicitly annotate things with their inferred types. It would be nice to know if the behavior here is somehow intentional or correct, or if it's a bug or limitation.
Activity