Skip to content

Commit e0cf9be

Browse files
authored
add example of augmenting HTMLElementTagNameMap to readme (#38)
1 parent e66afb3 commit e0cf9be

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,36 @@ _[Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAchgTzAUwCY
191191

192192
#### Custom Elements
193193

194-
If you passed an unknown tag, it will fall back to `Element`,
195-
but you can override it by specifying concrete type.
194+
If you passed an unknown tag, it will fall back to `Element`.
196195

197196
```typescript
198197
import 'typed-query-selector'
199198

200199
document.querySelector('my-web-component') // ==> Element
200+
```
201+
202+
However, you can override it by specifying a concrete type as a type parameter.
201203

204+
```typescript
202205
document.querySelector<MyComponent>('my-web-component') // ==> MyComponent
203206
```
204207

205208
_[Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAchgTzAUwCYFoCOBXNKZLAZzQBs0BjGaBAKHo2vIEMo04q2SS4BZZAGEI4CADs04+GgAeMKRj4AJACr8AMgFFKIKfADeAX0YYIVPHukA6fIWQBlCtVpQAFAhDEA7mgBGWFSikJLSCACUcAD0UXAAvHEAfHA6aFYwpuaW+rYERE6UNNAAPIIiYqEwiR5eWL4BQRX6EdGxCcllwRL6QA)_
206209

210+
Alternatively, you can use [global augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation) and [interface merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces) to extend `HTMLElementTagNameMap` with your custom element.
211+
212+
```typescript
213+
declare global {
214+
interface HTMLElementTagNameMap {
215+
'my-web-component': MyComponent;
216+
}
217+
}
218+
219+
document.querySelector('my-web-component') // ==> MyComponent
220+
```
221+
222+
_[Playground](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAchgTzAUwCYFoCOBXNKZLAZzQBs0BjGaBAKHo2vIEMo04q2SS4BZZAGEI4CADs04+GgAeMKRj4AJACr8AMgFFKIKfADeAX0bNu7TgHNyEAEatycA-Tiu4waYQBmrKpzWaOmh60qqslgByrHr8rGBOLm5JCCDEAO5otlhUopCS0ggAXALCuRL6ANyJriYmTBBUeCEwAHT4hMgAyhTUtFAAFCnpmdll+TAIAJRwAPQzcAC8CwB8JSJi40A)_
223+
207224
#### Invalid selector
208225

209226
When passing an invalid selector which causes parsing error,

0 commit comments

Comments
 (0)