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?
string
type Msg = string
string[]
type Msg = string[]
any
const msg: any = ...
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?
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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?
string, for example iftype Msg = string?string[], for example iftype Msg = string[]?any, i.econst msg: any = ...andconst 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?