UI.dev TypeScript Flashcards
What is the main goal of TypeScript?
To get rid of the problem of having variables of the wrong type.
What is TypeScript a combination of?
A compiler and a linter.
Any JavaScript program is also a valid TypeScript Program? Yah or Nah?
Yah - you don’t have to rewrite your whole app at the start.
In what way is TypeScript like ESLint?
It’s a static validation tool.
In addition to getting rid of having vairables of the wrong type - what is another goal of TypeScript?
To increase your confidence that the program behaves the way it is supposed to without unexpected bugs.
The thing I love about TypeScript is that when you run your code, you do it to see if it works as intended, not to see if it works.
Just a well put quote from Donovon West, that’s all. From Twitts.
TypeScript replaces ESLint and Automated Tests, right?
Nah, brah. It works with them to give you more confidence that the code is working correctly without having to run it - but it doesn’t replace them.
TypeScript is a run-time language.
No. All of the types are removed at compile time. It’s just there to encourage you to use the types to add chacks so you have fewer errors and bugs.
What escape hatches does TypeScript provide when you just can’t nail down a Type?
There’s both the Any type and @ts-ignore, which turns off the type checker for those parts of your code.
How does TypeScript help you navigate a codebase?
Type Annotation takes the guess work out of figuring out how a program works and serves as a guide to others in your codebase.
How does TypeScript help with refactoring?
It checks your change against any other files that might be affected and warns you if anything is broken. Then you just follow the warnings and update things. All without having to run the code!
Explain compile time vs. run time
Anything that happens before your code is executed is Compile Time - anything that happens after you execute your code is Run Time.
Explain Dynamic Types vs. Static Types
Languages like Java and C++ expect variables to always be the same type after they are declared. They use static, or unchanging, types.
Other languages, like JavaScript and Python, let you change the type of variables at runtime. These use dynamic types.
What is a Type Error?
This is when the program throws an error because a value of one type was used when the program was expecting another type. This happens when we use numbers in place of strings, or if we try to call a function, only to findout it is really undefined.
What is Type Safety?
Type Safety is eliminating type errors by ensuring types are only used in the correct place. This usually happens by having a compiler warn you when you use a type in the wrong way.
There are varying degrees of type safety, from incredibly rigid with a low chance of type errors to more flexible with a higher chance of type errors.
What is Static Analysis (aka Static Validation)?
This is when a tool analyzes your code without executing it. This can give you some insights into ways to improve your code and can warn you of errors before you run your code.
What the hey is a compiler?
A compiler is a program that takes code and transofrms it into another format.
Typically compilers convert code into machine code which is run directly by the CPU, but other compilers, like TypeScript, convert from one programming language (TypeScript) to another programming language (JavaScript)
What is a Type Annotation?
Just a small bit of code that tells TypeScript what type a value or variable is.
What is a Type Declaration?
A file with the .d.ts extension which only holds type definitions for a JavaScript library.
How are numbers stored in JavaScript?
As Floats - so you use the same type for both integers and decimal values. There are also values for infinity, negative infinity, and NaN.
What method can you use on the Number class to check if a number is imaginary or not?
Number.isNaN()
What is the BigInt type?
It was introduced in ES2019, and it lets you represent very large integers. It is created by putting a lowercase n at the end of our number literal:
const someBigNumber = 1337n
What is the deal with Symbols?
“They are unique and immutable. Every symbol you create is never going to equal another symbol, even if they are passed the same description as an argument.
”
What is the bug related to using typeof to check null?
“It instead returns ““object””, so if you’re checking for null values you shoudl do it with an equivalency instead:
”
What is Hyrum’s Law?
If there’s a bug within the system, someone will inevitably come to depend completely upon that bug.
What is the difference between a structured type and a primitive type?
A structured type shapes the primitive types to represent more complex data structures.
What is the basic structural type?
An object
How are structural types compared?
By reference, not by value. So, two objects with the exact same properties wil not equal each other.
What if we cloned a structured type and compared it to what we cloned it from?
They would be equal, because these are two instances of the same object.