TypeScript Flashcards
In dynamically typed languages, a variable’s type is determined when?
At runtime, based on the type of value it is assigned.
In statically typed languages, each variable must be assigned a specific data type when it is declared, and functions must specify the type of value they return.
In TypeScript, compile-time errors occur during…
the process of converting the TypeScript code into JavaScript code.
runtime errors occur in JavaScript when…
the code is being executed
The TypeScript compiler can detect both ____ and ____ before the code is executed.
syntax and type errors
What is type safety?
Type safety ensures that values in your code are used in a manner consistent with their declared types, helping catch potential errors at compile time rather than at runtime.
Changing existing JavaScript programs to TypeScript programs won’t “break” them, they will still compile and run. However, the TypeScript compiler will now throw a variety of helpful warnings and errors at build time, which we can resolve by updating our code and adding type annotations.
What are type annotations?
Type annotations are used to explicitly specify the type of the variable:
let myName: string = "Alice";
What will be logged to the console and why? How can we prevent this behavior?let numbers: Array<number> = [1, 2, 3, 4, 5];
let myNum: number = numbers[5];
console.log(myNum);
In the above code, we are able to assign the value undefined to the variable myNum, even though myNum has the type number. This seems like a bug, but TypeScript doesn’t raise a warning.
The issue is that TypeScript can’t know how long the array is, or will be. In other words, because JavaScript arrays have a dynamic length, the TypeScript compiler doesn’t know whether any particular index is out of bounds. As a result, it infers that any elements returned from the array are of the type the array contains.
You can add code to check if the variable is undefined
. Or you can use the noUncheckedIndexedAccess
compiler option in TypeScript.
What is the noUncheckedIndexedAccess
compiler option? Hint: .tsconfig
The noUncheckedInexedAccess
option is used to improve type safety by ensuring that all indexed access to object properties is properly checked. When this option is enabled, TypeScript treats every object property access using an index as potentially undefined, unless the type system can definitively determine that the property exists.
Tuples can be very useful in TypeScript when you want to…
work with a collection of values that have a specific order and data type.
let aTuple: [string, number, boolean] = ["Launch School", 1, true];
Primary differences between arrays and tuples?
Tuples have a fixed length.
You cannot access an out-of-bounds index of a tuple.
What happens if you call .push()
or .pop()
on a tuple? Why?
As long as you are pushing a valid type for the tuple, the TypeScript compiler will not raise an error. The problem is that tuples are just arrays, and thus they have all of the methods that arrays have. For this reason, it is generally considered a best practice to avoid using methods like push() and pop() when working with tuples as this can lead to unexpected behaviors.
This array consists of both string and boolean values. If you had to assign a type definition to myArray, what would it be?
const myArray = ["is", "launch school", "awesome", true, "or", false];
const myArray: (string | boolean)[] = [
"is",
"launch school",
"awesome",
true,
"or",
false,
];
____ is used as the return type for functions that don’t return a value.
void
What is type inference?
Type inference is a feature in TypeScript that allows the compiler to automatically deduce the types of variables when they are not explicitly specified.
What is meant by a method/function signature?
In TypeScript, a method signature (or function signature) defines:
1) the name of a method
2) the number and types of its parameters
3) the type of its return value.
It specifies how a method can be called, what arguments it takes, and what it returns, without providing the method’s implementation.
void
represents…
the absence of a value
Values with the void type cannot be…
1) assigned to any other data typefunction greet(name: string): void {
console.log(
Hello, ${name}!);
}
const result: void = greet("Jane");
// OKconst myUndefined: undefined = result; // Type 'void' is not assignable to type 'undefined'
2) cannot be used in a conditional to test truthiness.const myBool: boolean = result ? true : false; // An expression of type 'void' cannot be tested for truthiness.
Why does TypeScript raise an error with the following code?
function logSum(a: number, b: number): void {
const sum = a + b;
console.log("The sum of", a, "and", b, "is", sum);
return sum;
}
logSum(3, 4);
Because a function with a return type of void
is intended to not return a value and sum
is returned. If a function’s return type is void
and it has an explicit return it should only return undefined
, otherwise TypeScript will raise an error.
What is a literal type? List some specific examples.
In TypeScript literal types are used to describe the literal values that a variable can have.
Specific examples are string literals, numeric literals, and boolean literals.
let five: 5 = 5;
let hasName: true = true;
What is any
?
any
is a special TypeScript type that represents any possible value.
In TypeScript, type errors happen at…
compile-time
What is the following an example of?
type Person = { name: string; age: number };
The code snippet is an example of type aliasing.