class Lol {
    get length() {
        console.log('Lol. LMAO even.');
        return 5;
    }
}

const noLolsAllowed = (notALol: string) => {
    for (let i = 0; i < notALol.length; ++i) {
        console.log('No lols here');
    }
}

noLolsAllowed(new Lol() as unknown as string);
  • @DerPapa69@lemmy.ml
    link
    fedilink
    7
    edit-2
    1 year ago

    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
      link
      fedilink
      English
      61 year ago

      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@lemmy.ml
        link
        fedilink
        English
        61 year ago

        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!

    • @CannotSleep420OP
      link
      51 year ago

      I guess what I’m trying to say is that while Typescript is great, the types just make the development experience much nicer but don’t actually prevent any of the fuckery of Javascript. As for whether it’s strongly typed or not, I don’t find the term helpful. People call Typescript strongly typed, but is a language really strongly typed if you can tell it to ignore types? Strong is a relative term that doesn’t tell you much about the type system.

      • Ephera
        link
        fedilink
        English
        21 year ago

        Yeah, at this point, I’m basically using “strongly typed” as “How expressive/useful is the type system?”.

        Haskell, Rust, Scala, OCaml → Strongly typed.
        Python, JavaScript, C, Go → Worse than that.

        Personally, I would still put TypeScript in the strong category, because unless you are using as, it does have a helpful type system. A language preventing shitfuckery during runtime is kind of too late anyways.

        But yeah, sometimes even the best type system cannot express everything you know to be true as a human. Then you need to start casting. But casting can break during refactoring, so a language at least telling you during runtime that the cast is broken, is more helpful than a language just accepting shitfuckery. So, this does make TypeScript somewhat less strongly typed, even with my definition.

  • @kevincox@lemmy.ml
    link
    fedilink
    4
    edit-2
    1 year ago

    as is an escape hatch and super dangerous. I think they shouldn’t have given it such a nice name. unsafeCastAs would have been much better IMHO.

    It does require the uglier as unknown as in cases where it can prove that the cast is impossible. But that still allows many cases such as Foo|undefined as Bar|undefined working fine (TypeScript assumes undefined and doesn’t require the cast via unknown).

  • Dessalines
    link
    fedilink
    41 year ago

    God I hate webdev. And its even that much more unbearable without typescript.