TypeScript Exercises
Fill in the blanks to test your knowledge.
1
Add a type annotation to a string variable
const name: = "Alice";
2
Annotate a function parameter as a number
function double(x: ): number {
return x * 2;
}
3
Define an interface for a User object
User {
id: number;
name: string;
}
4
Create a generic function that returns its argument
function identity<>(arg: T): T {
return arg;
}
5
Define a union type for string or number
type ID = string number;
6
Use the as keyword for type assertion
const input = document.getElementById("box") HTMLInputElement;
7
Mark an interface property as optional
interface Config {
host: string;
port: number;
}
8
Define an array type of strings
const tags: [] = ["ts", "js"];