Learn TypeScript w/ Mike North

Game 3: Guess That Type

March 22, 2022

Table of Contents

1

ts
const values = [3, "14", [21]]
for (let a in values) {
// ^?
}
for (let b of values) {
// ^?
}
Click for answer
ts
const values = [3, "14", [21]]
 
for (let a in values) {
let a: string
}
for (let b of values) {
let b: string | number | number[]
}
Try

2

ts
class Person {
constructor(public name: string) {}
}
class Library {
librarians: Person[] = []
}
const localLibrary = Library
// ^?
const otherLibrary = new Library()
// ^?
Click for answer
ts
class Person {
constructor(public name: string) {}
}
class Library {
librarians: Person[] = []
}
 
const localLibrary = Library
const localLibrary: typeof Library
const otherLibrary = new Library()
const otherLibrary: Library
Try

3

ts
class AsyncManager {
constructor(arg: 0 | 4 | string) {
if (arg > 0 || arg <= 0) {
new Promise((resolve, reject) => {
arg
//^?
})
}
}
}
Click for answer
ts
class AsyncManager {
constructor(arg: 0 | 4 | string) {
if (arg > 0 || arg <= 0) {
new Promise((resolve, reject) => {
arg
(parameter) arg: string | 0 | 4
})
}
}
}
Try

4

ts
enum CoinResult {
heads,
tails,
}
function flipCoin(): CoinResult {
return Math.random() > 0.5
? CoinResult.heads
: CoinResult.tails
}
function main() {
const flipResult = flipCoin()
if (flipResult === CoinResult.heads) {
console.log("heads")
} else if (flipResult === CoinResult.tails) {
console.log("tails")
} else {
flipResult
// ^?
}
}
Click for answer
ts
enum CoinResult {
heads,
tails,
}
function flipCoin(): CoinResult {
return Math.random() > 0.5
? CoinResult.heads
: CoinResult.tails
}
 
function main() {
const flipResult = flipCoin()
if (flipResult === CoinResult.heads) {
console.log("heads")
} else if (flipResult === CoinResult.tails) {
console.log("tails")
} else {
flipResult
const flipResult: never
}
}
Try

5

ts
function getValue(): [number] | Promise<number> {
if (Math.random() > 0.5) return [42]
else return Promise.resolve(42)
}
async function main() {
const resolved = await getValue()
// ^?
}
Click for answer
ts
function getValue(): [number] | Promise<number> {
if (Math.random() > 0.5) return [42]
else return Promise.resolve(42)
}
 
async function main() {
const resolved = await getValue()
const resolved: number | [number]
}
Try

6

ts
let x: number | any = 41
const y = x
// ^?
Click for answer
ts
let x: number | any = 41
const y = x
const y: any
Try

7

ts
const values = [4, 1, null, 21, 45, 32]
const filtered = values.filter((val) => val !== null)
// ^?
Click for answer
ts
const values = [4, 1, null, 21, 45, 32]
 
const filtered = values.filter((val) => val !== null)
const filtered: (number | null)[]
Try

8

ts
class Person {
static species = "Homo Sapien"
constructor(public name: string) {}
}
const p = new Person("mike")
let x: keyof typeof Person
// ^?
let y: keyof typeof p
// ^?
Click for answer
ts
class Person {
static species = "Homo Sapien"
constructor(public name: string) {}
}
 
const p = new Person("mike")
let x: keyof typeof Person
let x: "prototype" | "species"
let y: keyof typeof p
let y: "name"
Try

9

ts
enum Language {
JavaScript,
TypeScript = "TS",
}
let lang1: Language = Language.JavaScript
// ^?
let lang2: Language = Language.TypeScript
// ^?
Math.round(lang1)
Math.round(lang2)
Click for answer
ts
enum Language {
JavaScript,
TypeScript = "TS",
}
 
let lang1: Language = Language.JavaScript
let lang1: Language
let lang2: Language = Language.TypeScript
let lang2: Language
 
Math.round(lang1)
Math.round(lang2)
Argument of type 'Language' is not assignable to parameter of type 'number'.2345Argument of type 'Language' is not assignable to parameter of type 'number'.
Try

10

ts
async function tryFetch(url: RequestInfo) {
try {
const val = await (await fetch(url)).json()
return val
} catch (err) {
console.error(err)
return undefined
}
}
async function main() {
const val = await tryFetch("https://example.com")
// ^?
}
Click for answer
ts
async function tryFetch(url: RequestInfo) {
try {
const val = await (await fetch(url)).json()
return val
} catch (err) {
console.error(err)
return undefined
}
}
 
async function main() {
const val = await tryFetch("https://example.com")
const val: any
}
Try

11

ts
class Book {
title: string = ""
author: string = ""
}
const keys = Object.keys(new Book())
// ^?
Click for answer
ts
class Book {
title: string = ""
author: string = ""
}
const keys = Object.keys(new Book())
const keys: string[]
Try


© 2022 All Rights Reserved