Yes, they are indeed different
Firstly, consider this example
let a: string;
let b: number;
function c() {
const d = (a || b) as number; // This works
const e: number = (a || b); // This throws a typing error
}
Hence, using as number
in TypeScript specifies that the value will be a number and defines the type of the result, essentially forcing TypeScript to assume it will always return a number even if that may not be the case.
On the other hand, ``: number`` defines the type of the variable, not the result. Therefore, TypeScript will ensure and verify that no other cases could occur, even though they may never happen.
I hope this explanation clears things up for you.