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
letreturnVoid: ()=>void;letreturnUndefined: ()=>undefined;constreturnNumber: ()=>number=()=>3;returnVoid=()=>{return2;// doesn't fail, but it should since number is not assignable to void};returnVoid=(): number=>{return2;// doesn't fail, but it should since number is not assignable to void};returnVoid=(): void=>{return2;// does fail because number is not assignable to void};returnVoid=()=>{console.log('test')}returnUndefined=()=>{return2;// does fail, because number is not assignable to undefined};returnUndefined=()=>{console.log('test')// does fail, because void is not assignable to undefined}returnVoid=returnNumber;// doesn't failreturnUndefined=returnNumber;// does fail
Expected behavior: () => number shouldn't be assignable to () => void
Actual behavior: () => number is assignable to () => void
Goal:
We want to enforce people to return nothing from that function (also return; should be possible). Maybe I'm not using the right keyword, but I didn't found anything around that. Also, we don't want people to have to specify : void when they declare the function since nowhere else it's required because typing are correctly behaving.
@j-oliveras Thanks for the link, I understand that use case, but would it be possible to have a new type like nothing (or whatever) that would make it possible to specify that we really want nothing?
But you already have that. If you explicitly annotate a void return on the function itself, Typescript does a stricter kind of check to handle exactly this use case, and any return <value> is errored.
But that requires the developer to put :void everywhere he is creating a new function for that specific function type. I'll probably have to live with it then.
TypeScript Version: 3.6.3
Search Terms:
Code
Expected behavior:
() => number
shouldn't be assignable to() => void
Actual behavior:
() => number
is assignable to() => void
Goal:
We want to enforce people to return nothing from that function (also
return;
should be possible). Maybe I'm not using the right keyword, but I didn't found anything around that. Also, we don't want people to have to specify: void
when they declare the function since nowhere else it's required because typing are correctly behaving.Playground Link
Related Issues: It might be related to #33420, but the end goal is not the same
The text was updated successfully, but these errors were encountered: