XJavascript.com
Toggle Menu
Home
Online Javascript Compiler
Tutorials
JavaScript Tutorials
TypeScript Tutorials
Blog
All Posts
TypeScript Basics
Check your understanding of TypeScript types,interfaces,and basic configurations.
1. What is the primary purpose of TypeScript?
Add dynamic typing to JavaScript
Add static typing to JavaScript
Replace JavaScript entirely
Improve JavaScript's runtime performance
2. Which of the following is a valid type annotation for a variable that holds a string?
let str: string = 'hello';
let str: String = 'hello';
let str = string 'hello';
string let str = 'hello';
3. What is the output of the TypeScript compiler (tsc) when compiling a .ts file?
Machine code
JavaScript code
HTML code
Bytecode
4. Which TypeScript type allows a variable to hold any value and disables type checking for that variable?
unknown
never
any
void
5. How do you declare a function in TypeScript that takes a number parameter and returns a number?
function add(n: number): number { return n + 1; }
function add(n) { return n + 1; }: number
number function add(n: number) { return n + 1; }
add(n: number): number => n + 1;
6. What is the 'unknown' type in TypeScript primarily used for?
Variables that will never hold a value
Variables with a type that is not known at compile time
Variables that can only hold null or undefined
Variables that must be initialized immediately
7. Which keyword is used to define a class in TypeScript?
class
define
type
interface
8. What does the 'as' keyword do in TypeScript?
Declares a variable
Defines a type alias
Performs type assertion
Imports a module
9. Which of the following are primitive types in TypeScript?
string
number
boolean
object
array
10. Which of the following are valid TypeScript types?
string
number[]
boolean
customType
any
11. Which statements about TypeScript interfaces are true?
Interfaces can be extended using the 'extends' keyword
Interfaces define the shape of objects
Interfaces can have optional properties using the '?' syntax
Interfaces are compiled into JavaScript code
Interfaces can contain function implementations
12. Which of the following are valid ways to specify an optional property in a TypeScript interface?
property?: type
property: type | undefined
optional property: type
property: optional type
13. Which features are introduced by TypeScript compared to vanilla JavaScript?
Static typing
Interfaces
Generics
Hoisting
Async/await
14. TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript code.
True
False
15. Using the 'any' type is recommended for most TypeScript projects to maximize flexibility.
True
False
16. Enums in TypeScript are compiled into objects in JavaScript.
True
False
17. TypeScript can infer the type of a variable if it is initialized when declared, eliminating the need for explicit type annotations in such cases.
True
False
18. What keyword is used to declare a type alias in TypeScript?
19. What is the standard file extension for TypeScript source files?
20. Name the command-line tool used to compile TypeScript code into JavaScript.
Reset
Answered 0 of 0 — 0 correct