0
0
Typescriptprogramming~10 mins

Conditional type with generics in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a generic type that returns string if the type is number, otherwise boolean.

Typescript
type CheckType<T> = T extends [1] ? string : boolean;
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of number in the condition.
Confusing the true and false branches of the conditional type.
2fill in blank
medium

Complete the code to create a conditional type that returns T if it extends string, otherwise returns never.

Typescript
type FilterString<T> = T extends [1] ? T : never;
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or other types instead of string.
Returning the wrong type in the true branch.
3fill in blank
hard

Fix the error in the conditional type that should return Array<T> if T extends string, else T.

Typescript
type WrapInArray<T> = T extends [1] ? Array<T> : T;
Drag options to blanks, or click blank then click option'
Aany
Bstring
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or other types in the extends clause.
Confusing the true and false branches.
4fill in blank
hard

Fill the blank to create a conditional type that returns Promise<T> if T extends string, otherwise returns T.

Typescript
type AsyncType<T> = T [1] string ? Promise<T> : T;
Drag options to blanks, or click blank then click option'
A==
Bimplements
Cextends=
Dextends
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or extends= which are invalid in this context.
Confusing the order of keywords.
5fill in blank
hard

Fill all three blanks to define a conditional type that returns string if T extends number, returns boolean if T extends string, otherwise returns never.

Typescript
type MultiCheck<T> = T [1] number ? string : T [2] string ? boolean : [3];
Drag options to blanks, or click blank then click option'
Aextends
Bimplements
Cnever
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using implements instead of extends.
Using any instead of never as fallback.