What is const cast in Typescript
Oct 13, 2021
Welcome back! Today I saw a strange syntax in Typescript.
return [Provider, useContext] as const
The strange part is “as const”. In Typescript, we can cast one type to another one by as keyword or <>.
let numStr = '1'
let num = numStr as Number
let anotherNum = <Number>numStr
In the above examples, we cast a string to a number. Back to our strange part. We know the const keyword makes an object immutable. So here we cast an array to an immutable type. For more detail, you can visit naddeo blog post or this github pull request.