mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-05 09:01:18 -04:00
chore(curriculum): update TypeScript quiz questions (#60002)
Co-authored-by: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com>
This commit is contained in:
@@ -234,7 +234,7 @@ Creating union types.
|
||||
|
||||
---
|
||||
|
||||
Creating intersection types.
|
||||
Defining types that include only specific values.
|
||||
|
||||
#### --answer--
|
||||
|
||||
@@ -332,7 +332,7 @@ Which file is typically used to configure TypeScript compiler options?
|
||||
|
||||
#### --text--
|
||||
|
||||
What does the `target` option in the `tsconfig.json` file specify?
|
||||
What does the `rootDir` option in the `tsconfig.json` file specify?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
@@ -348,51 +348,70 @@ The specific module system to use for imports and exports.
|
||||
|
||||
#### --answer--
|
||||
|
||||
The ECMAScript version to which the TypeScript compiler will transpile the code.
|
||||
The directory that holds your source files.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What does the `noImplicitReturns` option in the `tsconfig.json` file enforce?
|
||||
Which example uses `instanceof` correctly to narrow the type to access the `value` property safely?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
It forces all functions to return a `number`.
|
||||
```ts
|
||||
const email = document.querySelector("#email");
|
||||
if (email instanceof <input>) {
|
||||
console.log(email.value);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
It requires that all functions return either `true` or `false`.
|
||||
```ts
|
||||
const email = document.querySelector("#email");
|
||||
if (email instanceof Element) {
|
||||
console.log(email.value);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
It prevents using the `any` type in TypeScript code.
|
||||
```ts
|
||||
const email = document.querySelector("#email");
|
||||
if (email instanceof <HTMLInputElement>) {
|
||||
console.log(email.value);
|
||||
}
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
It ensures all code paths in a function return a value.
|
||||
```ts
|
||||
const email = document.querySelector("#email");
|
||||
if (email instanceof HTMLInputElement) {
|
||||
console.log(email.value);
|
||||
}
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which TypeScript option prevents implicit `any` types?
|
||||
What does setting `"strict": true` in `tsconfig.json` do in TypeScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`noImplicitReturns`
|
||||
Enables static type check so that you can catch the mistakes before you run your code.
|
||||
|
||||
---
|
||||
|
||||
`alwaysStrict`
|
||||
Enforces you to always use ES6 syntax.
|
||||
|
||||
---
|
||||
|
||||
`strictNullChecks`
|
||||
Enforces you to compile the code before you run it.
|
||||
|
||||
#### --answer--
|
||||
|
||||
`noImplicitAny`
|
||||
Enables a set of type-checking options such as requiring handling of `null` or `undefined` and preventing implicit `any`.
|
||||
|
||||
### --question--
|
||||
|
||||
@@ -420,51 +439,54 @@ To represent the type of values that never occur.
|
||||
|
||||
#### --text--
|
||||
|
||||
What will be the output of the following TypeScript code?
|
||||
Which of the following `getRandomValue` function calls will compile successfully in TypeScript?
|
||||
|
||||
```ts
|
||||
enum Color {
|
||||
Red,
|
||||
Blue,
|
||||
const getRandomValue = (array: string[]) => {
|
||||
return array[Math.floor(Math.random() * array.length)];
|
||||
}
|
||||
console.log(Color.Red, Color.Blue);
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`Red Blue`
|
||||
`getRandomValue([1, 2, 3])`
|
||||
|
||||
---
|
||||
|
||||
`1, 2`
|
||||
`getRandomValue("ABC")`
|
||||
|
||||
---
|
||||
|
||||
`Red 2`
|
||||
`getRandomValue({A: "A", B: "B", C: "C"})`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`0 1`
|
||||
`getRandomValue(["A", "B", "C"])`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
How can you specify that a function parameter is optional in TypeScript?
|
||||
How can you fix the TypeScript compiler error `'email' is possibly 'null'` in the following code using optional chaining?
|
||||
|
||||
```ts
|
||||
const email = document.querySelector<HTMLInputElement>("#email");
|
||||
console.log(email.value);
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
By using the `optional` keyword.
|
||||
`console.log(email?:value);`
|
||||
|
||||
---
|
||||
|
||||
By setting a default value.
|
||||
`console.log(email?value);`
|
||||
|
||||
---
|
||||
|
||||
By using the `any` type.
|
||||
`console.log(email.?value);`
|
||||
|
||||
#### --answer--
|
||||
|
||||
By adding a `?` after the parameter name.
|
||||
`console.log(email?.value);`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user