w3resource

TypeScript Basic - Exercises, Practice, Solutions

TypeScript basic [10 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

These exercises cover the fundamentals of TypeScript variables and data types, including declaration, type inference, type annotations, and working with arrays. They should help users become familiar with the basics of TypeScript's type system.

1. Write a TypeScript program that declares a variable `name` and assigns it a string value. Also declare a variable `age` and assign it a number value. Finally print the values of name and age.
Click me to see the solution

2. Write a TypeScript program that declares variables using let, const, and var. Then, describe how each declaration type behaves with respect to scoping and mutability.  
Click me to see the solution

3. Write a TypeScript program that declares variables of the following data types: number, string, boolean, and undefined. Assign values to them and perform basic operations.  
Click me to see the solution

4. Write a TypeScript program that declares a variable as a number and then tries to assign a string to it to see type checking in action.  
Click me to see the solution

5. Write a TypeScript program that declares a variable without specifying its type and shows how TypeScript infers the type based on the assigned value.  
Click me to see the solution

6. Write a TypeScript program that converts a variable of one type to another using type assertions and type conversion functions like parseInt().  
Click me to see the solution

7. Write a TypeScript program that creates type aliases for complex data types such as objects or custom types. Use them to define variables and explain how they improve code readability.  
Click me to see the solution

8. Write a TypeScript program that declares an array of a specific data type. It will demonstrates common array operations like adding elements, removing elements, and iterating through the array.  
Click me to see the solution

9. Write a TypeScript program that defines an enumeration 'Color' with values 'Red', 'Green', 'White' and Blue. Create a variable 'selectedColor' of type 'Color' and assign it one of the enumeration values.  
Click me to see the solution

10. Write a TypeScript program that declares a variable 'isNull' and assigns it null. You should also declare a variable called 'isUndefined' and assign it an undefined value.  
Click me to see the solution

More to Come !

TypeScript Editor

See the Pen TypeScript by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/typescript-exercises/typescript_basic_exercises.php