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
interfaceMessage{to: string;from: string;}interfaceMessageOptionalFrom{to: string;from?: string;}declarefunctionsend(data: Message|Message[]): Promise<void>;functiontest(x: MessageOptionalFrom){// This doesn't narrow because of // https://github.com/Microsoft/TypeScript/issues/21732if(!("from"inx)){thrownewError("");}// But this doesn't narrow either :(if(typeofx.from==="undefined"){thrownewError("");}// Or this :(if(x.from==null){thrownewError("");}returnsend(x);}
Expected behavior:
One of these approaches to narrowing should work such that, at the bottom of the function, x should be (MessageOptionalFrom & { from: string }), and the call to send should succeed.
Actual behavior:
Argument of type 'MessageOptionalFrom' is not assignable to parameter of type 'Message | Message[]'.
Type 'MessageOptionalFrom' is not assignable to type 'Message'.
Property 'from' is optional in type 'MessageOptionalFrom' but required in type 'Message'.
prop2 is correctly narrowed, but a.prop throws object possibly undefined HOWEVER the optional property is correctly narrowed if I remove the async keyword.
Realized that the root issue here is the same as #10065 and its various duplicates. Would be really nice to see this addressed. Since #10065 was closed and locked, I guess this is a decent place to track...
@Laubeee Your issue looks like something different (possibly #30625)
TypeScript Version: 3.2.0-dev.201xxxxx
Search Terms:
narrowing optional required property undefined
Code
Expected behavior:
One of these approaches to narrowing should work such that, at the bottom of the function,
x
should be(MessageOptionalFrom & { from: string })
, and the call tosend
should succeed.Actual behavior:
Playground Link:
http://www.typescriptlang.org/play/index.html#src=interface%20Message%20%7B%0D%0A%20%20to%3A%20string%3B%0D%0A%20%20from%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20MessageOptionalFrom%20%7B%0D%0A%20%20to%3A%20string%3B%0D%0A%20%20from%3F%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20function%20send(data%3A%20Message%20%7C%20Message%5B%5D)%3A%20Promise%3Cvoid%3E%3B%0D%0A%0D%0A%2F%2F%20Irl%2C%20this%20function%20would%20set%20the%20default%20%60from%60%2C%20%0D%0A%2F%2F%20or%20error%20if%20that's%20not%20possible%2C%20then%20send.%0D%0A%2F%2F%20For%20now%2C%20it%20always%20errors%20if%20from%20is%20missing.%0D%0Afunction%20test(x%3A%20MessageOptionalFrom)%20%7B%0D%0A%20%20%20%20%2F%2F%20This%20doesn't%20narrow%20because%20of%20%0D%0A%20%20%20%20%2F%2Fhttps%3A%2F%2Fgithub.com%2FMicrosoft%2FTypeScript%2Fissues%2F21732%0D%0A%20%20%20%20if%20(!(%22from%22%20in%20x))%20%7B%0D%0A%20%20%20%20%20%20%20%20throw%20new%20Error(%22%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%0D%0A%20%20%20%20%2F%2F%20But%20this%20doesn't%20narrow%20either%20%3A(%0D%0A%20%20%20%20if%20(typeof%20x.from%20%3D%3D%3D%20%22undefined%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20throw%20new%20Error(%22%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%2F%2F%20Or%20this%20%3A(%0D%0A%20%20%20%20if%20(x.from%20%3D%3D%20null)%20%7B%0D%0A%20%20%20%20%20%20%20%20throw%20new%20Error(%22%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%2F%2F%20x%20should%20be%20(MessageOptionalFrom%20%26%20%7B%20from%3A%20string%20%7D)%20here.%0D%0A%20%20%20%20%2F%2F%20Call%20below%20should%20work%2C%20therefore%3B%20errors%20instead.%0D%0A%20%20%20%20return%20send(x)%3B%0D%0A%7D
Related Issues:
#21732, but only covers one case.
The text was updated successfully, but these errors were encountered: