0
0
Typescriptprogramming~10 mins

Non-distributive conditional types 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 conditional type that checks if T is a string.

Typescript
type IsString<T> = T extends [1] ? true : false;
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cany
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' instead of 'string' in the extends clause.
Confusing the syntax of conditional types.
2fill in blank
medium

Complete the code to prevent distributive behavior by wrapping T in a tuple.

Typescript
type NonDistributive<T> = [T] extends [[1]] ? true : false;
Drag options to blanks, or click blank then click option'
Anumber
Bany
Cstring
Dunknown
Attempts:
3 left
💡 Hint
Common Mistakes
Not wrapping T in a tuple, causing distributive behavior.
Using the wrong type inside the extends clause.
3fill in blank
hard

Fix the error in the conditional type to avoid distribution over union types.

Typescript
type FixDistribution<T> = [T] extends [1] ? true : false;
Drag options to blanks, or click blank then click option'
Aboolean
Bstring
Cnumber
D[string]
Attempts:
3 left
💡 Hint
Common Mistakes
Using string directly, which causes distribution.
Not understanding how wrapping in tuples affects conditional types.
4fill in blank
hard

Fill both blanks to create a non-distributive conditional type that checks if T is assignable to U.

Typescript
type IsAssignable<T, U> = [T] extends [[1]] ? [2] : false;
Drag options to blanks, or click blank then click option'
AU
Btrue
CT
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Not wrapping T in a tuple.
Returning T instead of true in the true branch.
5fill in blank
hard

Fill all three blanks to create a non-distributive conditional type that returns the type if it extends U, else never.

Typescript
type FilterType<T, U> = [T] extends [[1]] ? [2] : [3];
Drag options to blanks, or click blank then click option'
AU
BT
Cnever
Dunknown
Attempts:
3 left
💡 Hint
Common Mistakes
Returning unknown instead of never in the false branch.
Not wrapping T in a tuple.