Loading topic
satisfies operator exists because of a trade-off between two things you already use every day: type inference and type annotations. This chapter reviews how each one decides what type a variable has. The later chapters assume you know exactly where inference gives you precision and where an an8 min readRecord. You should also be comfortable with the idea that TypeScript infers a type when you don't write one. Here you will see a problem that comes up in everyday code. You write a type annotation to make su7 min readRecord. You should also know how inference and annotations work. The previous chapter showed that a type annotation widens a value to the declared type. A common reaction is to reach for8 min readsatisfies operator. It assumes you know TypeScript's basic types, interfaces and type aliases, union and literal types, Record, and the difference between a type annotation and an inferred type. The previous chapters showed that annotations lose precision and that `as6 min readRecord works. It also assumes you have seen the satisfies syntax from the previous chapters. Here you will learn the second job of satisfies. It does not only check an expression. It also hands the target ty7 min readsatisfies: finding misspelled and unexpected property names in object literals. It assumes you know interfaces, type aliases, optional properties, union types, and the difference between a type annotation and an as assertion. The earlier chapters in this tutorial c8 min readRecord, keyof, union and literal types, and the basic satisfies syntax from earlier chapters. Here we focus on one job: forcing an object to contain exactly the keys you expect, with the right value type for each key, without losing the precise types of the values6 min readsatisfies does at a basic level and have seen Record, keyof, typeof and union types. You should also know that as const exists, but it is explained again here. By the end you will be able to write as const satisfies T, derive union types from the result8 min read: T), a type assertion (as T), and the satisfies operator. This chapter compares them and gives you a procedure for picking one.7 min readsatisfies syntax, how annotations widen types, how as const works, and how excess property checks behave on object literals. You also need strict mode turned on, which is the setting all the examples assume.7 min read