Loading topic
"up" accepts the string "up" and nothing else. The type 42 accepts the number 42. This chapter covers what literal types are, where they come from, how they relate to string and number, and which mistakes they cause. The c7 min readstring, number, boolean, object types) and can annotate function parameters and return types. It also assumes you can read switch statements and object literals in JavaScript. The previous chapter introduced literal types, which are typ6 min readstring | number. It builds on the previous chapters on literal types and unions of literals. Here you will learn why the same string literal gets the type "up" in one declaration and `6 min readswitch work in JavaScript. It also assumes you have read the previous chapter, which explained why let widens a literal to string while const keeps "up". Here you will learn how to 6 min readswitch statements and object literals. Earlier chapters covered literal types and unions of literals. Here you will look at the first kind of enum TypeScript offers, the numeric enum. It is the d6 min readas const, numeric and string enums, and what enums compile to. Here you use that knowledge to make a decision. Both 6 min read"up" | "down", and that you have seen enums and discriminated object shapes. Compile everything with strict turned on. Several results below depend on it.6 min read${} syntax as JavaScript template strings, but they work on types. They let you derive a union such as "onClick" | "onFocus" from "click" | "focus" instead of writing every member by hand.6 min readconst enum is an enum whose members the compiler replaces with their literal values, so no enum object exists at runtime. This chapter covers what that changes in the emitted JavaScript, the restrictions it adds, and why many projects avoid it.8 min readas const, and the typeof/keyof pattern for deriving a union from a constant object. You should also be comfortable with Record<K, V> and with the difference between a type annotation and a6 min read