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

TypeScript never type #113

Closed
basarat opened this issue May 25, 2016 · 3 comments
Closed

TypeScript never type #113

basarat opened this issue May 25, 2016 · 3 comments

Comments

@basarat
Copy link
Owner

basarat commented May 25, 2016

PR : microsoft/TypeScript#8652
Issue : microsoft/TypeScript#3076
Concept : https://en.wikipedia.org/wiki/Bottom_type

Add bottom.md in tips.

Use case

// Without a bottom type we would error : 
// - Not all code paths return a value
// - Unreachable code detected
function foo(x: string | number) { 
  if (typeof x === "string") { 
    return true; 
  } else if (typeof x === "number") { 
    return false; 
  }
  fail("Unexhaustive!"); 
} 

function fail(message: string) { throw message; }

Confusion with void

void is a Unit. never is a falsum.

A function that returns nothing returns a Unit void. However a function that never returns (or always throws) returns never. void is something that can be assigned (wihout strictNullChecking) but never can never be assigned to anything other than never.

@basarat
Copy link
Owner Author

basarat commented Jul 27, 2016

Docs for the Never Type live here : https://basarat.gitbooks.io/typescript/content/docs/types/never.html 🌹

@Cryrivers
Copy link

i think the use case is not valid anymore. As I found that TypeScript doesn't infer throw or infinite loop to be bottom type never as the "return" value unless you explicitly declare it.

image

@basarat
Copy link
Owner Author

basarat commented Jan 2, 2018

The annotation needs to be explicit now : fc3b4ca 🌹

The default inference is now void.

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

No branches or pull requests

2 participants