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
Inconsistencies in type checking functions with optional destructured parameters #17667
I encountered unexpected and inconsistent errors when attempting to write a function declaration for a function with an optional destructured parameter with default property values when using strictNullChecks.
It's possible that I wrote my function declaration incorrect but I couldn't find a good example for writing function declarations with an optional destructured parameter with default property values. Regardless, the inconsistency in behavior described in this issue is still a bug AFAICT.
lethave: (nums?: {a?:number,b?:number})=>void;have=({a =1, b =2}={})=>{};// A. Results in error.constwant=({a =1, b =2}={})=>{};have=want;// B. This works fine.
Expected behavior:
The lines denoted by A. and B. both work (preferable).
OR
The lines denoted by A. and B. fail in the same way.
Actual behavior:
The line denoted by A. results in errors:
Type '{ a?: number | undefined; b?: number | undefined; } | undefined' has no property 'a' and no string index signature.
Type '{ a?: number | undefined; b?: number | undefined; } | undefined' has no property 'b' and no string index signature.
have = want; fails now with --strictFunctionTypes. seems like we are not adding | undefined for optional parameters correctly for want.
so there are two issues here, not one.
A. A contextually-typed binding pattern doesn't check initialised properties to the type correctly. It should understand the structure-with-initialiser well enough to narrow out the undefineds from the contextual type.
B. A non-contextually-typed binding pattern should add | undefined to properties that it knows are optional because they have initialisers. Right now it only gets marked as optional.
In the meantime, both bugs can be worked around by adding an explicit type annotation to the destructured parameter itself: const want = ({ a = 1, b = 2 }: { a?: number, b?: number} = {}) => {};
I have fixed both issues present here and the changes are just about ready.
Still working on some nice testcases and stuff.
Expect a PR in the next days. Just saying, so that nobody starts putting work in. :-D
Activity
whatisaphone commentedon Oct 3, 2017
I wonder if #17080 is related to this?
mhegazy commentedon Oct 10, 2017
have = want;fails now with--strictFunctionTypes. seems like we are not adding| undefinedfor optional parameters correctly forwant.so there are two issues here, not one.
trevorade commentedon Oct 10, 2017
I mean, at the end of the day, what I want to be able to do is write something like this:
The
have = wantthing is a hack to work around the issue.If my declaration for
adderis off, I'm happy to change it, I just don't know how to write it such that the TypeScript compiler is happy.sandersn commentedon Oct 10, 2017
I think what Mohamed Hegazy (@mhegazy) is saying is that you found two bugs:
A. A contextually-typed binding pattern doesn't check initialised properties to the type correctly. It should understand the structure-with-initialiser well enough to narrow out the undefineds from the contextual type.
B. A non-contextually-typed binding pattern should add
| undefinedto properties that it knows are optional because they have initialisers. Right now it only gets marked as optional.sandersn commentedon Oct 10, 2017
In the meantime, both bugs can be worked around by adding an explicit type annotation to the destructured parameter itself:
const want = ({ a = 1, b = 2 }: { a?: number, b?: number} = {}) => {};Lazarus535 commentedon Jan 26, 2018
I have fixed both issues present here and the changes are just about ready.
Still working on some nice testcases and stuff.
Expect a PR in the next days. Just saying, so that nobody starts putting work in. :-D
Fixes microsoft#17667
Lazarus535 commentedon Jan 26, 2018
PR is out! :-)
jakebailey commentedon Jul 8, 2023
Just looking at old issues; this one was fixed by #37309 (and #37271 was the same just years later).