as unknow as number
is known as a Double Assertion. unknown
is compatible with all types (like any
) and equally unsafe as as any as
, but you're only allowed to operate with the value until a specific type is determined. I don't know (yet) vs I don't care. Linters prefer unknown (with no-explicit-any rule)
Typescript: Why does `as unknown as x` work gives this example:
data as Item
will only work if data
can be assigned to Item
OR
Item
can be assigned to data
. Since this is not true data as Item
is an error.
data as unknown
works because anything can be assigned to
unknown
and therefore data
can be assigned to unknown
. => 1
unknown as Item
works because again anything can be assigned to
unknown
and therefore Item
can be assigned to unknown
. => 2
data as unknown as Item
works because 1 && 2 are allowed.