Type System Basics

Preview — 3 of 10 questions

What is the correct way to annotate a variable in TypeScript?

javascript
// Which is valid?
let name = "Alice";           // A
let name: string = "Alice";   // B
let name<string> = "Alice";   // C
let name::string = "Alice";   // D
Alet name = "Alice" — no annotation needed
Blet name: string = "Alice" — colon syntax
Clet name<string> = "Alice" — angle bracket syntax
Dlet name::string = "Alice" — double colon syntax

What type does TypeScript infer for score?

javascript
let score = 42;
score = "high"; // ?
Aany — TypeScript can't infer from a number literal
Bnumber — TypeScript infers number from the initializer, so assigning a string is an error
Cnumber | string — TypeScript infers a union because both values are assigned
D42 — TypeScript infers the literal type

Which two syntaxes are equivalent for typing an array of strings?

javascript
// Option 1
let tags: string[];

// Option 2
let tags: Array<string>;
AThey are different — string[] is read-only, Array<string> is mutable
Bstring[] only works with primitives; Array<string> works with all types
CArray<string> allows null elements; string[] does not
DThey are equivalent — both declare a mutable array of strings

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.