Skip to content

Commit 6818b85

Browse files
Update patterns_by_version.md
1 parent a0ff9ab commit 6818b85

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

docs/advanced/patterns_by_version.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,82 @@ let stuff = (
445445
let stuff = h(Fragment, null, h("div", null, "Hello"));
446446
```
447447

448-
Possibly in 4.1
448+
## TypeScript 4.1
449449

450-
- 4.1 plan https://github.com/microsoft/TypeScript/issues/40124
451-
- [Recursive Conditional Types](https://github.com/microsoft/TypeScript/pull/40002)
450+
[[Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html) | [Blog Post](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/)]
451+
452+
1. Template Literal Types
453+
454+
This is a HUGE feature.
455+
456+
Usecase 1 - Generating string literal types from permutations of other string literal types:
457+
458+
```tsx
459+
type VerticalAlignment = "top" | "middle" | "bottom";
460+
type HorizontalAlignment = "left" | "center" | "right";
461+
462+
// Takes
463+
// | "top-left" | "top-center" | "top-right"
464+
// | "middle-left" | "middle-center" | "middle-right"
465+
// | "bottom-left" | "bottom-center" | "bottom-right"
466+
declare function setAlignment(value: `${VerticalAlignment}-${HorizontalAlignment}`): void;
467+
468+
setAlignment("top-left"); // works!
469+
setAlignment("top-middel"); // error!
470+
setAlignment("top-pot"); // error! but good doughnuts if you're ever in Seattle
471+
```
472+
473+
Usecase 2 - Modeling dynaming string literal types:
474+
475+
```tsx
476+
type PropEventSource<T> = {
477+
on(eventName: `${string & keyof T}Changed`, callback: () => void): void;
478+
};
479+
480+
/// Create a "watched object" with an 'on' method
481+
/// so that you can watch for changes to properties.
482+
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>;
483+
```
484+
485+
To make string manipulation easier there are new generics: `Uppercase`, `Lowercase`, `Capitalize` and `Uncapitalize`.
486+
487+
2. [React 17 jsx Factories](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#react-17-jsx-factories)
488+
489+
This is a new compiler option to offer output inline with React 17 support in general:
490+
491+
```jsx
492+
// ./src/tsconfig.json - for production
493+
{
494+
"compilerOptions": {
495+
"module": "esnext",
496+
"target": "es2015",
497+
"jsx": "react-jsx",
498+
"strict": true
499+
},
500+
"include": [
501+
"./**/*"
502+
]
503+
}
504+
505+
// ./src/tsconfig.dev.json - for development - extending the production config
506+
{
507+
"extends": "./tsconfig.json",
508+
"compilerOptions": {
509+
"jsx": "react-jsxdev"
510+
}
511+
}
512+
```
513+
514+
515+
Misc
516+
517+
2. [Key Remapping in Mapped Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#key-remapping-in-mapped-types)
518+
3. [Recursive Conditional Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#recursive-conditional-types)
519+
4. [Checked Indexed Accesses](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#checked-indexed-accesses-nouncheckedindexedaccess)
520+
521+
## TypeScript 4.2
522+
523+
Plan - https://github.com/microsoft/TypeScript/issues/41601
452524

453525
## TypeScript Roadmap and Spec
454526

0 commit comments

Comments
 (0)