Buscar
Estás en modo de exploración. debe iniciar sesión para usar MEMORY

   Inicia sesión para empezar

level: Level 1

Questions and Answers List

level questions: Level 1

QuestionAnswer
What type will TypeScript infer for "age"? const age = 66 Because the age variable is a const, it is the most specific type TypeScript can associate with it.
When we associate a type to a variable declaration (like endTime below), what is that called? let endTime: DateType annotation
In the function declaration below, what are the "a" and "b" called? function add(a: number, b: number) { ... }Function parameters
In the code below, what are "3" and "4" called? add(3, 4)Arguments
What are the two key parts of Object type definitions?1. The names of the properties that are (or may be) present 2. The types of those properties
How would you change the code below to make chargeVoltage an optional property? function printCar(car: { make: string model: string year: number chargeVoltage: number })Add the optional operator (?) ie: chargeVoltage?: number
Put into words what an index signature is useful for.Index signatures are used to describe the type of a key used to retrieve an object when working with dictionaries.
In the declaration below, what is "UserContactInfo" called ? type UserContactInfo = { name: string email: string }A type alias
Which of the following you would use to augment an existing type? Type Alias Interface BothInterface
True or False: If we have a value that is a union type, we can only access members that are common to all types in the union.True
When would a literal type be used?For allowing an exact value which a string, number, or boolean must have. e.g., const constantString = "Hello World"
Is TypeScript’s type system static or dynamic? When does that mean the type-checking occurs?Static, occurs at compile time (as opposed to runtime)
What is the basic rule for TypeScript's structural type system?x is compatible with y if y has at least the same members as x. interface Pet { name: string; } let pet: Pet; // dog's inferred type is { name: string; owner: string; } let dog = { name: "Lassie", owner: "Rudd Weatherwax" }; pet = dog;
What's going on here? function handleMainEvent( elem: HTMLFormElement, handler: FormSubmitHandler ) function handleMainEvent( elem: HTMLIFrameElement, handler: MessageHandler ) function handleMainEvent( elem: HTMLFormElement | HTMLIFrameElement, handler: FormSubmitHandler | MessageHandler ) {}This is an example of function overloading. Two function heads or overload signatures are written above the body of the function and serve as multiple entry points to a single implementation.
Name TypeScript's four built-in data structure typesObject Record Tuple Array
What is this type called? type TrafficLight = CanCross | ShouldStop;Union Remember: it is either a value of type X or of type Y
What is this type called? type Foo = A & B;Intersection Remember: It is a value that is simultaneously of type A and B
Which type occurs when we intersect two types that do not overlap at all?never Which is the empty set.
How would you rewrite this function to be generic without using "any"? function identity(arg: any): any { return arg }By using generic type-parameters function identity<T>(arg: <T>): <T> { return arg }
How is the type for an array of strings written?string[]
If TypeScript doesn't have enough information around the declaration site to infer what a variable type should be, what type does the variable get?any, the most flexible type (the normal way JS variables work)
What issue does TypeScript's excess property checking address?Throws an error when using an object literal that has a property that does not exist in the type that the function expects, meaning there's no way to safely access the property later on.
What are three ways to fix an error about excess property checking?1. Remove the excess property 2. Add the property to the function argument type 3. Create a variable to hold the value, and pass the variable (instead of the object literal) into the function
Define a tuple.A multi-element, ordered data structure, where the position of each item has some special meaning or convention
What is the difference between nominal and structural type systems? Describe how this applies to type checking.Nominal type systems are about names. Structural ones are about structure or shape. The type equivalence check on a function call checks whether the argument is an instance of a class of a given name if nominal. With structural, it doesn't care about which constructor its argument came from, only whether it has the necessary properties.
How do type aliases help address the complexity of many properties on a type?1. Define a more meaningful name for the type 2. Declare the particulars of the type in a single place 3. Allows import and export of this type from modules
What are type guards?Expressions which, when used with a control flow statement (e.g., instanceof), allow us to have a more specific type for a particular value