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
TS doesn't allow optional param destructing in spite of having a default value assigned #35160
typeF={(foo?: {bar?: {}}): void}constf: F=({ bar }={})=>{}constg: F=(foo={})=>{const{ bar }=foo}
Expected behavior:
Both functions: f and g should be compiled without errors. foo is optional, but it's also provided a default value; same with bar. Therefore both foo and bar are in fact never undefined. Btw this also works in vanilla js, tested with all sorts of parameter types.
Actual behavior:
TS error is thrown for the f function's destructuring of the optional param:
Property 'bar' does not exist on type '{ bar?: {} | undefined; } | undefined'.(2339)
This error does sound right, but shouldn't occur taking into consideration those three facts:
If foo is undefined (= not passed as an argument to the function), the default value kicks in, so foo becomes {}. It's safe to access bar then.
Function g does exactly the same logic as f, yet it doesn't fail compilation.
Ryan Cavanaugh (@RyanCavanaugh) Exactly, so adding | undefined to bar in my example doesn't change anything since bar is optional. I don't see how this is relevant to my issue anyway. What do you mean by:
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
TypeScript Bot (@typescript-bot) It's still not clear to me whether this really is a question or a bug, as my last comment hasn't been answered. Please don't close until there's a unanimous conclusion about it.
Activity
RyanCavanaugh commentedon Nov 26, 2019
TypeScript is doing what you told it to do: you wrote
barwithout any annotation, so it was assumed to be a required destructuring.You can write this instead if you meant it to be optional:
jalooc commentedon Nov 27, 2019
Ryan Cavanaugh (@RyanCavanaugh)
baris typed withing theFtype. Isn't that enough?Besides
?implies it can beundefined. Isn't addingundefinedtype to the union for an optional param a no-op?RyanCavanaugh commentedon Nov 27, 2019
Contextual typing can change the type of a parameter, but can't change its optionality.
| undefinedis implied by optionality, but not vice versa.jalooc commentedon Nov 27, 2019
Ryan Cavanaugh (@RyanCavanaugh) Exactly, so adding
| undefinedtobarin my example doesn't change anything sincebaris optional. I don't see how this is relevant to my issue anyway. What do you mean by:? It's type is derived from
F.typescript-bot commentedon Dec 2, 2019
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
jalooc commentedon Dec 2, 2019
TypeScript Bot (@typescript-bot) It's still not clear to me whether this really is a question or a bug, as my last comment hasn't been answered. Please don't close until there's a unanimous conclusion about it.