Typescript is strongly typed

<pre style="background-color:#ffffff;">
<span style="color:#323232;">class Lol {
</span><span style="color:#323232;">    get length() {
</span><span style="color:#323232;">        console.log('Lol. LMAO even.');
</span><span style="color:#323232;">        return 5;
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">const noLolsAllowed = (notALol: string) => {
</span><span style="color:#323232;">    for (let i = 0; i < notALol.length; ++i) {
</span><span style="color:#323232;">        console.log('No lols here');
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">noLolsAllowed(new Lol() as unknown as string);
</span>
DerPapa69,
@DerPapa69@lemmy.ml avatar

I don’t really get the point of this. Of course the function will accept the value as a string if you specifically tell it to treat it as such (which wouldn’t even be possible without casting it to unknown first)?

Ephera,

I guess the point is, it’s not strongly typed during runtime. In other strongly typed languages, that cast would fail, since the underlying memory layout of some random class does not fit to that of a string.

But yeah, as soon as you break out as, you’re telling the type system to fuck off. So, while it does look freaky to me, too, it doesn’t dispell that TypeScript is strongly typed…

DerPapa69,
@DerPapa69@lemmy.ml avatar

Yeah, I do agree that it’s a bit weird with TS. It’s fully understandable though, since it just transpiles to JS, which doesn’t have any type information during runtime. I think as far as webdev goes though, TS makes it at least somewhat bearable.

Sorry for the downvote btw - that wasn’t me!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines