Master TypeScript’s powerful generics with 30 MCQs designed to boost your understanding. Dive into generic functions, classes, interfaces, and constraints to enhance your TypeScript skills and flexibility.
function name<T>(value: T): T { return value; }function <T> name(value: T): T { return value; }function name<T>(value) { return value; }generic function<T> name(value: T): T { return value; }function identity(value): T { return value; }function identity<T>(value: T): T { return value; }function identity(value: T): T { return value; }function<T> identity(value: T): T { return value; }<T> represent in a generic function?
identity<string>("Hello World");identity<string>()("Hello World");identity("Hello World");identity("Hello World"): string;function<T>(value: T): T { return value; }function identity<T>(value: T): T { return value; }function identity(value: T): T { return value; }function identity<T> (value) { return value; }any typeoutput be? function echo<T>(value: T): T { return value; } const output = echo(5);
stringnumberanyTfunction myFunction<T, U>(param1: T, param2: U): T { return param1; }function myFunction<T|U>(param1: T, param2: U): T { return param1; }function myFunction<T U>(param1: T, param2: U): T { return param1; }function myFunction<T, U>(param1, param2): T { return param1; }class Box<T> { value: T; }class Box { T: type; }class Box<T> { T value; }class Box { value: T; }value: T;T value;T: value;value: T<Type>;const box = new Box<number>();const box = new Box();const box = Box<number>();const box = Box();interface Container<T> { value: T; }interface Container { value: T; }interface Container<T> { value: string; }interface<T> Container { value: T; }const container: Container<string> = { value: "Hello" };const container = { value: "Hello" };const container: Container = { value: "Hello" };const container: string = { value: "Hello" };<T extends Type><T is Type>T: TypeT => TypeT extends ClassNameT is ClassNameT implements ClassNameT = ClassNameT extends { length: number } do?function getLength<T extends { length: number }>(item: T): number { return item.length; }
T to types that have a length propertyT is always a stringT to types that are numbersT as a string onlyfunction merge<T extends { name: string }>(object: T) { return object; }function merge<T extends string>(object: T) { return object; }function merge<T>(object: T) { return object; }function merge(object: T) { return object; }T extends number?
T will default to numbernumber automaticallyT will be forced to a stringid properties?
function getId<T extends { id: number }>(object: T): number { return object.id; }function getId<T extends { id: string }>(object: T): number { return object.id; }function getId<T extends { id: number }>(object): T { return object.id; }function getId(object: T): number { return object.id; }extends keyword& operatoror operatorA and B?
<T extends A & B><T extends A or B><T extends A | B><T extends A, B>T[] extends ObjectT extends Array<any>extends in a generic constraint?
<T extends string | number><T is string | number><T extends string & number><T extends string | number[] ><T extends { length: number }><T extends string | number><T extends { length: number; toString(): string }>extends constraint in a generic function ensure?
any typestring| Qno | Answer |
|---|---|
| 1 | b) To enable the reuse of a function with different data types |
| 2 | a) function name<T>(value: T): T { return value; } |
| 3 | b) function identity<T>(value: T): T { return value; } |
| 4 | a) A placeholder for any type |
| 5 | b) Enabling dynamic type assignment |
| 6 | a) identity<string>("Hello World"); |
| 7 | b) function identity<T>(value: T): T { return value; } |
| 8 | a) TypeScript infers the type automatically |
| 9 | b) number |
| 10 | a) function myFunction<T, U>(param1: T, param2: U): T { return param1; } |
| 11 | a) class Box<T> { value: T; } |
| 12 | a) value: T; |
| 13 | a) const box = new Box<number>(); |
| 14 | a) interface Container<T> { value: T; } |
| 15 | a) const container: Container<string> = { value: "Hello" }; |
| 16 | a) A way to restrict the types that can be passed to a generic function |
| 17 | a) <T extends Type> |
| 18 | a) Restricts T to types that have a length property |
| 19 | a) Restricts T to types that have a length property |
| 20 | a) function merge<T extends { name: string }>(object: T) { return object; } |
| 21 | a) Only numbers can be passed to the function |
| 22 | a) function getId<T extends { id: number }>(object: T): number { return object.id; } |
| 23 | a) Yes, using the extends keyword |
| 24 | a) <T extends A & B> |
| 25 | a) Yes, using the array constructor |
| 26 | a) It specifies that the type must be a subclass |
| 27 | a) `<T extends string |
| 28 | d) All of the above |
| 29 | a) The generic type conforms to the specified type’s properties or methods |
| 30 | a) It uses the type provided at the time of function call |