Practique preguntas conocidas
Manténgase al día con sus preguntas pendientes
Completa 5 preguntas para habilitar la práctica
Exámenes
Examen: pon a prueba tus habilidades
Modo de examen no disponible
Aprenda nuevas preguntas
Modos dinámicos
InteligenteMezcla inteligente de todos los modos
PersonalizadoUtilice la configuración para ponderar los modos dinámicos
Modo manual [beta]
Seleccione sus propios tipos de preguntas y respuestas
Modos específicos
Aprende con fichas
TypeScript - Marcador
TypeScript - Detalles
Niveles:
Preguntas:
27 preguntas
🇬🇧 | 🇬🇧 |
What type will TypeScript infer for "age"? const age = 6 | 6 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: Date | Type annotation |
In the function declaration below, what are the "a" and "b" called? function add(a: number, b: number) { ... } | Function parameters |
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. |
Which of the following you would use to augment an existing type? Type Alias Interface Both | Interface |
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" |
You’re hovering over the variable name for an array of strings. What does the type look like? | String[] |
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 types | Object 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 } |
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 |
How would the type of a car be written with properties make (string), model (string), and year (number)? | { make: string model: string year: number } |
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 |