DEV Community

Discussion on: Type `Assertions` vs `Declarations` - the dangers of `as` in Typescript

Collapse
 
andrekovac profile image
André Kovac

Thanks for this very nice example! I understand the difference for complex types (objects, unions etc.)

Is there a difference of a type assertion and a type declaration in the following three cases?

  1. For primitive types like string, for example if type Msg = string?
  2. For Arrays of primitive types like string[], for example if type Msg = string[]?
  3. In case of any, i.e const msg: any = ... and const msg = ... as any?

If there is no difference there is no danger in using a type assertion in these cases, right?

P.S. You use the term "type declaration" - you're referring to a "type annotation", right?