Challenges 7 exercises
Problem

Dynamically Typing Arguments

Internationalization Library Challenge

Here we have a set of translations that include the name of a person using the app and the number of unread messages.


const translations = {
title: "Hello, {name}!",
subtitle: "You have {count} unread messages.",
button: "Click me!",

Loading exercise

Transcript

00:00 In this challenge, we are doing an internationalization library. We have a set of translations here that are basically being marked as const and they're doing that so that you can capture the literals from them. They have some various dynamic args here, that you can basically say, hello name,

00:18 and that'll be the name of the person that's using your app. You have however many unread messages. The idea is that this translate function up here will take in this object of translations, take in the key, and then this is the tricky bit. Depending on whether it has any dynamic stuff in it, so any of these,

00:37 then it should optionally give you an argument to basically say, okay, you have to pass in the count here. If you use subtitle, you have to pass in count. If you use title, you have to pass in title. But if you use button, then you shouldn't have to pass in anything. In fact, it should error if you

00:56 try to pass in something that's not required. We have a dynamic number of arguments here based on the thing that's being passed in. I have made it a little easier for you in that I've given you a type here called getParamKeys, which basically takes in a translation string and returns

01:14 a tuple with all of those things in, basically with all the translations in. This will help you a little bit and should give you a way to type these args better, I think. I think that's all I'm going to give you. You're probably definitely going to need a couple of

01:32 type arguments on the translate function, possibly two to three, depending on how you implement it. This is a neat little challenge and definitely has a lot of applications in the real world, so good luck.