You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because Typescript is not Go, <string>bb means "please consider bb as a string even if it isn’t". It does not (and cannot) check whether it is actually valid. When you use this notation, it's your responsibility for it to make sense. If you want runtime checks, you can write them using typeof.
Reminder: Because type assertions are removed at compile-time, there is no runtime checking associated with a type assertion. There won’t be an exception or null generated if the type assertion is wrong.
For the record, the term "type assertion" is maybe a bit misleading - it's not an "assertion" in the same sense as assert(typeof foo === 'string') - it's you asserting that the value is actually the type you say, regardless of what the compiler currently thinks it is. You won't get a runtime error if you're wrong, because the type information is completely erased by the compiler.
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.
Suggestion
There should be an error when the type assertion fails
typescript
golang
The text was updated successfully, but these errors were encountered: