Passing Type Arguments 7 exercises

Specify a Default Value

The solution here is to use a default generic.

Recall how setting a default worked with a type:

type CreateSet<T = string> = Set<T>;

With the syntax above, if we were to create a new Result type from CreateSet without passing in a type argument, Result would end up as a s

Loading solution


0:00 The solution here is to use a default generic. Just like if we had a type CreateSet type helper, where we would say Set and we'd say it's a T here. Then we would say type Result = CreateSet<number>. We can actually just not pass anything into here and default this to string. Result would be set to string.

0:25 We can use this syntax and just stick it up here and now this is going to work because it knows that if you don't pass a type argument, then its default value should be string. Otherwise, it's going to default to unknown. This works exactly the same way in this context, as it does in a type helper context.