Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Literal types

Literal types allow us to declare the exact value that a string Boolean or number must have. When we declare a variable using the let keyword, its value will be inferred as a primitive type:

let five = 5; // numberlet falsy = false; // booleanlet shape = "rectangle"; // string

However, if we use the const keyword, the type is inferred as the actual assigned value:

const five = 5; // 5const falsy = false; // falseconst shape = "rectangle"; // rectangle

Literal types can be used in combination with union types, type guards, and type aliases with ease:

type ShapeKind = "square" | "rectangle" | "circle";

Literal types can be used in combination with type guards and the power of control flow analysis to narrow union types using a ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content