Skip to content

Type is not assignable to type 'void' warning inconsistent #34675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jimmyfortinx opened this issue Oct 23, 2019 · 4 comments
Closed

Type is not assignable to type 'void' warning inconsistent #34675

jimmyfortinx opened this issue Oct 23, 2019 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jimmyfortinx
Copy link

TypeScript Version: 3.6.3

Search Terms:

Code

let returnVoid: () => void;
let returnUndefined: () => undefined;
const returnNumber: () => number = () => 3;

returnVoid = () => {
    return 2; // doesn't fail, but it should since number is not assignable to void
};

returnVoid = (): number => {
    return 2; // doesn't fail, but it should since number is not assignable to void
};

returnVoid = (): void => {
    return 2; // does fail because number is not assignable to void
};

returnVoid = () => {
    console.log('test')
}

returnUndefined = () => {
    return 2; // 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 fail
returnUndefined = 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.

Playground Link

Related Issues: It might be related to #33420, but the end goal is not the same

@j-oliveras
Copy link
Contributor

@jimmyfortinx
Copy link
Author

jimmyfortinx commented Oct 23, 2019

@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?

@nmain
Copy link

nmain commented Oct 23, 2019

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.

@jimmyfortinx
Copy link
Author

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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants