Beginner's TypeScript Section 18 exercises
solution

Add Types to Object Params

There are multiple solutions to this problem.

Solution 1: Pass an Object Type Directly

Perhaps the simplest solution is to pass an object type directly to the params argument.

Use curly braces to represent the object, then create type inline:

export const addTwoNumbers = (params
Loading solution

Transcript

0:00 There are multiple solutions to this problem. The first one, and maybe one of the simplest to understand, is you can parse in an object type here direct to the params argument. Use these curly braces to represent and object, and then you create the type in line there.

0:18 You notice that TypeScript will understand if you use a comma in this situation, but you mostly, and Prettier will correct it, so you use semicolons, instead.

0:29 This is the first solution. The second solution is by creating a type here. We create a named type, give a type a name, which is pretty useful. We create it with this equals sign here. We say...basically parse the same thing as we had before, just inside a type there.

0:47 If I wanted to convert this to a type, I'll just create a new type here and say NewType and say type NewType = and just paste in what I copied there.

0:58 The third solution is to use an interface. An interface is pretty similar to type, but interfaces can only be used to represent objects and a couple of other things, but mostly objects in this basic course.

1:12 You may be wondering why would you choose type over interface? Type can technically represent anything. Type could be a number. It can be a string. It can be boolean. It can be absolutely anything, whereas an interface, if you try to say interface = string, it's going to yell at you. It's not going to let you. It's not very fun.

1:34 Why would you use interface over type? There used to be a lot of confusion over this. It's a question I get asked a lot.

1:41 To be honest, whatever syntax you prefer, you should choose and just stay consistent. Either one is going to give you...

1:49 You'll notice, too, if you...In this first solution, if I refactor this back to how it was, I'm actually going to get a more verbose error message if I do this rather than the other way. If I mess this up, and if I say...Let's say I remove one of these and we just add in a random number instead.

2:08 Then, it's going to say "Argument of type 'number' is not assignable to parameter of type..." and it gives it the literal value here, so it's spelled out.

2:16 Whereas if I do it on this one, where it has a type alias assigned to it and I go blah, blah, blah, blah, blah, blah, then it's going to say, "Argument of type 'number' is not assignable to type 'AddTwoNumbers Args'", which is better and a bit more readable, a bit more understandable.

2:32 In general, you should be extracting these out into their own aliases, but it doesn't really matter whether you use type or interface, especially when you're first starting out.