Added TypeScript/Unknown Type (#26181)

This commit is contained in:
Cuong Nguyen
2018-12-08 21:35:07 +02:00
committed by Christopher McCormack
parent 5d5a29649f
commit 7ecf3e3efc

View File

@ -0,0 +1,31 @@
---
title: Unknown Type
---
## Undefined Type
In TypeScript, `unknown` type is a type-safe counterpart of `any`, where anything can be assigned to `unknown` but cannot assign to anything except itself and `any`.
```typescript
const unknownValue = 1
let x: unknown = unknownValue
x = 2 // type-safe
const y: number = x // type-safe fails
```
An `unknown` type should be casted to a specific type to be used.
```typescript
const matchUnknown = (x: unknown) => {
if (typeof x === 'string') {
return x.toUpperCase()
}
if (typeof x === 'number') {
return x * 2
}
throw new Error(`type of ${x} is not supported`)
}
```