beamgrid
Back to home

TypeScript Behaviors

A reference for every question, with code and detailed explanations.

1

What is the type of `Result`?

TypeScript
'Definitely not'

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Open in Playground
2

What will TypeScript do if you write the code below?

TypeScript
TypeScript won't raise any errors

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.

Open in Playground
3

What is the output of `UserWithoutBirthdate`? (assume that 'exactOptionalPropertyTypes' option in tsconfig is set to true)

TypeScript
{ name: string; age: number; }

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Open in Playground
4

What's the type of `Result`?

TypeScript
number | string

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.

Open in Playground
5

What does TypeScript infer as the return type of this function?

TypeScript
number

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Open in Playground
6

What is the type of `Result` in the code below involving `never` type?

TypeScript
[never, true]

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Open in Playground
7

What will TypeScript do in the following scenario with a faulty type guard?

TypeScript
TypeScript will compile without errors and the inferred type of user inside the if statement will be `string`

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

Open in Playground
8

What will TypeScript do in the following scenario with a faulty assertion function?

TypeScript
TypeScript will raise an error because after the function call `user` type is `never`, so it's not possible to use `toLowerCase()`

Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur.

Open in Playground
9

What will TypeScript infer for the type of `UserWithoutName`?

TypeScript
{ name: string; age: number; }

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.

Open in Playground
10

What is the type of `NewUser`?

TypeScript
{ name: string; age: never; }

Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam.

Open in Playground
11

What is the type of `NameType`?

TypeScript
string

Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur.

Open in Playground
12

What is the type of `Result`?

TypeScript
{ value: string | number; }

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum.

Open in Playground
13

What is the type of `Keys`?

TypeScript
"name" | "age" | "id"

Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus.

Open in Playground
14

What is the type of `arr`?

TypeScript
(number | string | boolean)[]

Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet.

Open in Playground
15

What is the type of `RedType`?

TypeScript
number[]

Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores.

Open in Playground
16

What is the type of `TupleType`?

TypeScript
readonly [1, 2, 3]

Nulla facilisi. Morbi tempus iaculis urna id volutpat lacus laoreet non curabitur gravida.

Open in Playground
17

What will TypeScript do with the code below?

TypeScript
TypeScript won't raise any errors

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

Open in Playground
18

What is the type of `Handler`?

TypeScript
"onClick" | "onFocus"

Viverra accumsan in nisl nisi scelerisque eu ultrices vitae auctor eu augue ut lectus.

Open in Playground
19

What is the type of `Result`?

TypeScript
string[]

Eget nulla facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus.

Open in Playground
20

What is the type of `Result`?

TypeScript
number

Amet consectetur adipiscing elit pellentesque habitant morbi tristique senectus et netus.

Open in Playground