add more documentation
This commit is contained in:
1 parent
dcef2470cb
commit
b86c9d915e
8 files changed
+129
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
# Type Programming in TypeScript
|
||||
|
||||
This example showcases advanced type-level programming in TypeScript using template literal types and recursive type definitions.
|
||||
|
||||
We can define a type that parses a string literal at compile-time to extract placeholders and construct a corresponding object type.
|
||||
|
||||
```typescript
|
||||
type ParseUrlParams<Url> = ...;
|
||||
|
||||
function navigate<P extends string>(path: P, params: ParseUrlParams<P>) {
|
||||
// ...
|
||||
}
|
||||
|
||||
navigate("/book/{author}/{id}", { author: "foo", id: "bar" }); // Correct
|
||||
navigate("/book/{author}/{id}", { author: "foo" }); // -> 💥 Compiler Error: missing 'id'
|
||||
```
|
||||
|
||||
This allows for extremely type-safe APIs, where the parameters required for a function are derived directly from a string template, ensuring that developers provide all necessary data.
|
||||
Reference in new issue
Block a user