0
0
Typescriptprogramming~5 mins

Parameters type in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does 'Parameters type' mean in TypeScript?
It means specifying the type of values that a function expects as inputs, so the function knows what kind of data it will receive.
Click to reveal answer
beginner
How do you write a function in TypeScript that takes a number as a parameter?
You write the parameter name followed by a colon and the type. Example: <pre>function double(x: number) { return x * 2; }</pre>
Click to reveal answer
beginner
What happens if you pass a wrong type to a function with typed parameters in TypeScript?
TypeScript will show an error before running the code, helping you fix the mistake early.
Click to reveal answer
intermediate
Can function parameters in TypeScript have optional types?
Yes, you can mark parameters as optional with a question mark. Example: <pre>function greet(name?: string) { ... }</pre>
Click to reveal answer
beginner
What is the benefit of using parameter types in functions?
It helps catch mistakes early, makes code easier to understand, and improves code quality by ensuring correct data is used.
Click to reveal answer
How do you specify a string parameter named 'text' in a TypeScript function?
Afunction example(text) {}
Bfunction example(string text) {}
Cfunction example(text = string) {}
Dfunction example(text: string) {}
What does the question mark (?) mean in a parameter like 'name?: string'?
AThe parameter is optional
BThe parameter is required
CThe parameter is a number
DThe parameter is a default value
What will TypeScript do if you pass a number to a function expecting a string parameter?
ACrash the program at runtime
BConvert the number to string automatically
CShow a type error before running
DIgnore the type and run anyway
Which of these is a correct way to define a function with two parameters: a number and a boolean?
Afunction check(num, flag) {}
Bfunction check(num: number, flag: boolean) {}
Cfunction check(number num, boolean flag) {}
Dfunction check(num: boolean, flag: number) {}
Why is it helpful to use parameter types in functions?
ATo catch errors early and understand code better
BTo make code run faster
CTo allow any type of input without errors
DTo make functions shorter
Explain how to define a function with typed parameters in TypeScript and why it is useful.
Think about how you tell the function what kind of data it should expect.
You got /2 concepts.
    Describe what happens if you pass the wrong type of argument to a function with typed parameters in TypeScript.
    Consider how TypeScript helps you avoid bugs.
    You got /2 concepts.