How TypeScript Infers a Variable’s Type
Give a variable a value, and TypeScript infers its type from it: let pages = 320 is a number from then on.
- Programming
- TypeScript
Give a variable a value, and TypeScript infers its type from it: let pages = 320 is a number from then on.
Write : never on a function that always throws, and TypeScript knows the code after a call to it only runs when the call didn’t happen.
Write a type in angle brackets after a generic type’s name, such as Array, and tsc only lets values of that type in.
After if (!value) return, TypeScript knows the value isn’t null below the check, so the possibly null error goes away.
Write type in front of an imported type, so a tool that removes the types removes the import too.
Write a colon and a type after the parameter’s name, and tsc reports any call that passes the wrong kind of value.
After upgrading a TanStack Start app to TypeScript 7, npm run build failed with a TypeError about ‘Intrinsic’. Here’s the one check that showed why, and the side-by-side fix from TypeScript’s announcement.
JSON.parse works in TypeScript just as in JavaScript: it turns a JSON string into an object or an array, and throws a SyntaxError for invalid JSON.