Connect, collaborate, and grow with a community of TypeScript developers
new(challenge): default-generic-arguments (#961)
Dimitri Mitropoulos0be230267f11
diff --git a/challenges/default-generic-arguments/metadata.json b/challenges/default-generic-arguments/metadata.jsonnew file mode 100644index 00000000..55372d09--- /dev/null+++ b/challenges/default-generic-arguments/metadata.json@@ -0,0 +1,7 @@+{+ "id": "default-generic-arguments",+ "label": "Default Generic Arguments",+ "description": "Default generic arguments allow you to optionally pass a generic argument.",+ "difficulty": "beginner",+ "prerequisites": ["generic-type-arguments", "type-unions", "generic-constraints", "indexed-types"]+}diff --git a/challenges/default-generic-arguments/prompt.md b/challenges/default-generic-arguments/prompt.mdnew file mode 100644index 00000000..0ffdd650--- /dev/null+++ b/challenges/default-generic-arguments/prompt.md@@ -0,0 +1,68 @@+## Why Generic Arguments Need Defaults++Technically speaking, no program absolutely requires default arguments. It's a convenience feature for the humans writing programs.++Sometimes you have a value in mind for an argument and you don't want to force users of your library to constantly provide the same value over and over again. That's the situation where default arguments come in handy.++## How To Create Default Type Arguments++### First: A Refresher On Function Defaults++Let's take the case of a run-of-the-mill logging function. The logger always needs a message to log, but [log level](https://en.wikipedia.org/wiki/Syslog#Severity_level) might be something you want to be optional:++```ts+type LogLevel = 'debug' | 'info' | 'notice' | 'warning' | 'error' | 'critical';++const log = (message: string, level: LogLevel = 'info') => {+ // application logic+}++log('this has an explicit debug log level', 'debug');+log('this has an implicit info debug level');+```++You can do exactly this kind of thing for generic types! This is yet another parallel between functions and generic types!++### Default Generic Arguments++```ts+type Log<Message, Level = 'info'> = {+ message: Message;+ level: Level;+}++type ExplicitDebugLog = Log<"explicit debug", "debug">;+type ImplicitInfo = Log<"implicit info">;+```++Doesn't that look super similar!? A lot of the same rules apply in types that apply to function argument defaults. For example, you can't have a required argument following a default argument.++One notable exception is that there's no TypeScript value you can pass that will work like `undefined` does for JavaScript default arguments:++```ts+const greet = (name = 'Stranger') => {+ console.log(`Hello ${name}!`);+};++greet(); // Hello Stranger!+greet(undefined); // Hello Stranger!+greet("Mr. Monkey") // Hello Mr. Monkey!+```++In TypeScript, even if you provide `never` or `unknown` or `any`, the value will be inserted instead of the default.++## Solving This Challenge++Your goal is to create some types that have have the correct default argument.++The second type, `TSConfig`, should feel a bit difficult, but if you get stuck, feel free to check out the little hint below:++<details>++<summary>small hint</summary>++1. Start with a literal object as the parameter default.+1. Use [indexed types](https://typehero.dev/challenge/indexed-types).+1. The error you'll see is because of a missing [generic constraint](https://typehero.dev/challenge/generic-type-constraints).++</details>diff --git a/challenges/default-generic-arguments/solutions/1.ts b/challenges/default-generic-arguments/solutions/1.tsnew file mode 100644index 00000000..aa9e6231--- /dev/null+++ b/challenges/default-generic-arguments/solutions/1.ts@@ -0,0 +1,8 @@+type ApiRequest<Data, Method = 'GET'> = {+ data: Data;+ method: Method;+};++type TSConfig<Config extends { strict: boolean } = { strict: true }> = {+ strict: Config['strict'];+}\ No newline at end of filediff --git a/challenges/default-generic-arguments/tests.ts b/challenges/default-generic-arguments/tests.tsnew file mode 100644index 00000000..f2acebf6--- /dev/null+++ b/challenges/default-generic-arguments/tests.ts@@ -0,0 +1,33 @@+import { Expect, Equal } from 'type-testing';++type test_ApiRequest_explicitPost = Expect<Equal<+ ApiRequest<string, 'POST'>,+ { data: string; method: 'POST' }+>>;++type test_ApiRequest_implicitGet = Expect<Equal<+ ApiRequest<number>,+ { data: number; method: 'GET' }+>>;++type test_TSConfig_default = Expect<Equal<+ TSConfig,+ { strict: true }+>>;++type test_TSConfig_true = Expect<Equal<+ TSConfig<{ strict: true }>,+ { strict: true }+>>;++type test_TSConfig_false = Expect<Equal<+ TSConfig<{ strict: false }>,+ { strict: false }+>>;++type test_TSConfig_boolean = Expect<Equal<+ TSConfig<{ strict: boolean }>,+ { strict: boolean }+>>;++diff --git a/challenges/default-generic-arguments/tsconfig.json b/challenges/default-generic-arguments/tsconfig.jsonnew file mode 100644index 00000000..875cb600--- /dev/null+++ b/challenges/default-generic-arguments/tsconfig.json@@ -0,0 +1,3 @@+{+ "compilerOptions": {}+}diff --git a/challenges/default-generic-arguments/user.ts b/challenges/default-generic-arguments/user.tsnew file mode 100644index 00000000..20e7a364--- /dev/null+++ b/challenges/default-generic-arguments/user.ts@@ -0,0 +1,3 @@+type ApiRequest = unknown;++type TSConfig = unknown;