Skip to content
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

Remove misconception about what never is assignable to. #638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dwjohnston
Copy link
Contributor

And because never is only assignable to another never

This isn't true!

never is assignable to a number for example, see for yourself.



function myFunction(a: number) {

}


function returnsNever() : never {
    throw new Error(); 
}


const a= returnsNever(); 
myFunction(a); // No type error here!

https://www.typescriptlang.org/play?ssl=15&ssc=1&pln=1&pc=1#code/FDBmFcDsGMBcEsD2kAEBbAngMSnJkAKAQwC4VJw0AjAUwCcBKFAbxAF8QxcFkU6bY4OpADOAORoA3egSZlIU+i2ApVKWAAs6iAO7kaegKJ1tdWQG4UwDiGjIRsFEQC8fAUNETpZhpeCYcGB5CIl8UAHpwlDFEdQwABxoUelMUDXoaAEJgIA

See documentation here:
https://www.typescriptlang.org/docs/handbook/basic-types.html#never

The never type is a subtype of, and assignable to, every type;

>And because `never` is only assignable to another `never` 

This isn't true!

`never` _is_ assignable to a number for example, see for yourself. 

```


function myFunction(a: number) {

}


function returnsNever() : never {
    throw new Error(); 
}


const a= returnsNever(); 
myFunction(a); // No type error here!

```

https://www.typescriptlang.org/play?ssl=15&ssc=1&pln=1&pc=1#code/FDBmFcDsGMBcEsD2kAEBbAngMSnJkAKAQwC4VJw0AjAUwCcBKFAbxAF8QxcFkU6bY4OpADOAORoA3egSZlIU+i2ApVKWAAs6iAO7kaegKJ1tdWQG4UwDiGjIRsFEQC8fAUNETpZhpeCYcGB5CIl8UAHpwlDFEdQwABxoUelMUDXoaAEJgIA

See documentation here: 
https://www.typescriptlang.org/docs/handbook/basic-types.html#never

>The never type is a subtype of, and assignable to, every type;
@dwjohnston
Copy link
Contributor Author

I think you want to update your fail function to what I've got here for a proper exhaustiveness check:

https://stackoverflow.com/a/67684250/1068446

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant