You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
globalPreamble: "declare var onChange: () => void;declare var console: { log: Function };\n"
8
-
preambles:
9
-
- file: select-1.tsx
10
-
text: "import React from 'react';\n"
11
-
- file: select-2.tsx
12
-
text: "import React from 'react';\n"
13
-
- file: select-3.tsx
14
-
text: "import React from 'react';\n"
15
-
lib:
16
-
- react
17
-
note: '
18
-
When I wrote this piece, I realized it would be much easier to explain if I could show you how the TypeScript compiler handles the code I’m talking about, as if we were both sitting in front of your favorite code editor with language support. So, I built this blog to imitate that experience. Hover (or tap) on code underlined in red to see errors, hover identifiers to see type information, and even edit the samples if you want. I also <a href="/overengineering-a-blog-prologue-an-ode-to-red-squigglies/">wrote more</a> about why I did this.
19
-
20
-
21
-
Disclaimer: I wrote this before joining the TypeScript team. I’m not speaking officially for TypeScript or Microsoft in
22
-
any post on this blog, but I’m especially not in this one.'
7
+
note: |
8
+
This post has been archived and delisted. The code examples relied on my
9
+
<a href="/overengineering-a-blog">previous blog infrastructure</a>, which showed
10
+
live TypeScript errors and type information. Ultimately, that was not worth the
11
+
upkeep and has been removed. Consequently, this post may not make sense without
12
+
seeing the errors that are supposed to be rendered. The content also refers to
13
+
very old versions of TypeScript, React, and <code>@types/react</code> and may
14
+
no longer be accurate.
23
15
---
24
16
25
17
One of TypeScript’s most underrated features is _discriminated union types_. Borrowed primarily from functional programming (FP) languages, they match an elegant FP concept to a pattern people intuitively write in JavaScript. Discriminated unions also enable a useful pattern for typing complex React component props more safely and expressively. But first, we’ll review what discriminated unions look like independent of React.
This post describes the infrastructure of this blog before a more recent rewrite,
10
+
where I removed all the cool features described herein. The blog is now made with
11
+
<a href="https://11ty.dev">eleventy</a> and doesn’t have interactive code blocks.
12
+
Ultimately, it wasn’t worth the upkeep.
8
13
---
9
14
10
15
<spanclass="small-caps">It’s all about the red squigglies.</span> Red squigglies in Microsoft products have been saving me from looking stupid since the second grade, and now they save me from writing stupid code on a minutely basis. Well, significantly less stupid code. You know the red squigglies I’m talking about? The red squigglies that tell you that no, it’s not spelled “bureauacracy,” and, more frequently for me lately, that object is possibly `undefined`.
Copy file name to clipboardExpand all lines: content/posts/archive/2019-02-16_Overengineering_A_Blog.md
+10-2
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,11 @@ date: 2019-02-16
4
4
tags: archive
5
5
permalink: overengineering-a-blog/
6
6
layout: post
7
-
note: 'Disclaimer: I wrote this before joining the TypeScript team. I’m not speaking officially for TypeScript or Microsoft in any post on this blog, but I’m especially not in this one.'
7
+
note: |
8
+
This post describes the infrastructure of this blog before a more recent rewrite,
9
+
where I removed all the cool features described herein. The blog is now made with
10
+
<a href="https://11ty.dev">eleventy</a> and doesn’t have interactive code blocks.
11
+
Ultimately, it wasn’t worth the upkeep.
8
12
lib:
9
13
- react
10
14
preambles:
@@ -135,7 +139,11 @@ I’m telling this piece of the story a little out of order, because I think it
135
139
136
140
<figure>
137
141
{% image "./images/bundle-analysis.png", "A graphical representation of the blog’s assets by size, as provided by webpack-bundle-analyzer. TypeScript takes up the entire left half of the chart, plus some." %}
138
-
<figcaption>[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) shows TypeScript’s size relative to everything else.</figcaption>
142
+
<figcaption>
143
+
144
+
[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) shows TypeScript’s size relative to everything else.
145
+
146
+
</figcaption>
139
147
</figure>
140
148
141
149
I also realized that none of the really heavy pieces are necessary until the moment a reader starts _editing_ a code sample. I could analyze the initial code at build time, generate syntax highlighting information, type information, and compiler diagnostics as static data, inject it with GraphQL, and reference it during render, all for a fraction of the weight of the compiler and syntax highlighter itself.
Copy file name to clipboardExpand all lines: content/posts/archive/2019-08-05_Writing_Type_Safe_Polymorphic_React_Components_(Without_Crashing_TypeScript).md
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;\n
18
-
"
19
-
preambles:
20
-
- file: GoodButton.tsx
21
-
text: "
22
-
declare function mergeProps<T extends {}[]>(...props: T): {
23
-
[K in keyof UnionToIntersection<T[number]>]:
24
-
K extends 'className' ? string :
25
-
K extends 'style' ? UnionToIntersection<T[number]>[K] :
26
-
Extract<T[number], { [Q in K]: unknown; }>[K]; }\n
27
-
"
28
-
lib:
29
-
- react
30
-
- dom
31
-
compilerOptions:
32
-
lib:
33
-
- /lib.es2015.d.ts
34
-
- /lib.dom.d.ts
8
+
note: |
9
+
This post has been archived and delisted. The code examples relied on my
10
+
<a href="/overengineering-a-blog">previous blog infrastructure</a>, which showed
11
+
live TypeScript errors and type information. Ultimately, that was not worth the
12
+
upkeep and has been removed. Consequently, this post may not make sense without
13
+
seeing the errors that are supposed to be rendered. The content also refers to
14
+
very old versions of TypeScript, React, and <code>@types/react</code> and may
15
+
no longer be accurate.
35
16
---
36
17
37
18
<spanclass="small-caps">When designing a React component for reusability</span>, you often need to be able to pass different DOM attributes to the component’s container in different situations. Let’s say you’re building a `<Button />`. At first, you just need to allow a custom `className` to be merged in, but later, you need to support a wide range of attributes and event handlers that aren’t related to the component itself, but rather the context in which it’s used—say, `aria-describedby` when composed with a Tooltip component, or `tabIndex` and `onKeyDown` when contained in a component that manages focus with arrow keys.
0 commit comments