Skip to content

Commit adbe5ca

Browse files
committed
Add more information on unknown/never as any substitutes
1 parent f59b25c commit adbe5ca

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs/typescript.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,23 @@ const handler:
222222

223223
#### Fixes for `any`
224224

225-
##### Try `unknown` instead
225+
##### Try `unknown` and `never` instead
226226

227-
`unknown` is the universal supertype i.e. the widest possible type.
227+
###### `unknown`
228228

229-
- When typing the assignee, `any` and `unknown` are interchangeable (every type is assignable to both).
230-
- However, when typing the assigned, `unknown` can't be used to replace `any`, as `unknown` is only assignable to `unknown`. In this case, try `never`, which is assignable to all types.
229+
- `unknown` is the universal supertype i.e. the widest possible type.
230+
- Every type is assignable to `unknown`, but `unknown` is only assignable to `unknown`.
231+
- When typing the _assignee_, `any` and `unknown` are completely interchangeable since every type is assignable to both.
232+
- `any` usage is often motivated by a need to find a placeholder type that could be anything. `unknown` is the most likely type-safe substitute for `any` in these cases.
233+
234+
##### `never`
235+
236+
- `never` is the universal subtype i.e. the narrowest possible type.
237+
- `never` is assignable to every type, but the only type that is assignable to `never` is `never`.
238+
- When typing the _assigned_:
239+
- `unknown` is unable to replace `any`, as `unknown` is only assignable to `unknown`.
240+
- The type of the _assigned_ must be a subtype of the _assignee_.
241+
- `never` is worth trying, as it is the universal subtype and assignable to all types.
231242
<!-- TODO: Add example -->
232243

233244
##### Don't allow generic types to use `any` as a default argument

0 commit comments

Comments
 (0)