Skip to content

TypeScript never type  #113

Closed
Closed
@basarat

Description

@basarat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions