Why does 'any' exist in TypeScript?

Поділитися
Вставка

КОМЕНТАРІ • 34

  • @ayoaloko1453
    @ayoaloko1453 5 місяців тому +15

    love the fit. Going from typescript king to drip king

  • @DanielAWhite27
    @DanielAWhite27 5 місяців тому +16

    Unknown should (my preference: must) be used over any

    • @seroltech118
      @seroltech118 5 місяців тому

      It must.

    • @robertsandiford6223
      @robertsandiford6223 5 місяців тому +5

      Not always possible, especially when dealing with generics

    • @DanielAWhite27
      @DanielAWhite27 5 місяців тому

      @@robertsandiford6223 trying to think of an example. Do you have one?

    • @soniablanche5672
      @soniablanche5672 5 місяців тому

      Wrong. unknown can be a pain in the ass and you'd have to write 5 millions lines of code to narrow the type. Not worth wasting time writing that code and the potential performance hit.

    • @DanielAWhite27
      @DanielAWhite27 5 місяців тому

      @@soniablanche5672then just use JavaScript directly

  • @davidspagnolo4870
    @davidspagnolo4870 5 місяців тому +4

    Because every real language has a footgun.

  • @orterves
    @orterves 5 місяців тому +7

    According to our front-end developers, the appropriate time to use any is any time you can't be bothered to create a type for a semi complicated value from the backend.

  • @KristianTheDesigner
    @KristianTheDesigner 5 місяців тому

    Any is litterally my nemesis at work atm , hehe. looking forward to the next vid.

  • @BlurryBit
    @BlurryBit 5 місяців тому +1

    Looking forward to it. I hate the any keyword and I would never use it personally. But since you are saying it has its use cases, I am intrigued, coz you are the one whom I learned typescript from. 😂

  • @nomadshiba
    @nomadshiba 2 місяці тому

    never and unknown together are just better alternatives

  • @not_hehe__
    @not_hehe__ 5 місяців тому

    its so we can just put any everywhere and add typescript to our resume

  • @Zutraxi
    @Zutraxi 5 місяців тому

    Typescript is hard to fathom for some juniors. Because they feel like they declare classes. But then suddenly at runtime they they a string instead of a number in a value because of their API returned it. And it contains other values not used.
    Makes it really confusing for them. Sometimes I wish it wasn't so.

  • @robertsandiford6223
    @robertsandiford6223 5 місяців тому

    I want to know if there is a better way to do things like:
    const array: GenType[] = [ ..stuff.. ]
    We need 'any' to be able to put different GenType values into the array, but using 'any' breaks internal type checking in those items.

    • @mattpocockuk
      @mattpocockuk  5 місяців тому

      satisfies

    • @robertsandiford6223
      @robertsandiford6223 5 місяців тому

      @@mattpocockuk my current approach is
      const array = [
      { first: 'A', second: 'B' } satisfies GenType,
      { first: 'C', second: 'D' } satisfies GenType
      ] as const satisfies GenType[]
      But it's very wordy and repetitive

    • @robertsandiford6223
      @robertsandiford6223 5 місяців тому

      @@mattpocockuk Realised I was being a bit dumb, and can do
      satisfies GenType[]

  • @rhatalos1997
    @rhatalos1997 Місяць тому

    It's better to use an occasional any than to ditch TypeScript because it's to frustrating.

  • @george_davituri
    @george_davituri 5 місяців тому

    👍🏻

  • @orderandchaos_at_work
    @orderandchaos_at_work 5 місяців тому

    You cold?

  • @ReasonX3
    @ReasonX3 5 місяців тому

    TypeScript describes itself as a superset of JavaScript. In other words TS adds new features to JS, but still stays compatible with it. This is one of the reasons why TypeScript became so popular - it can be used seamlessly with JS, which means you can migrate from JS to TS at any moment and learning curve is very gradual. Hell, you can simply change file extension from ".js" to ".ts" and everything will work without any refactor. 100% strict & mandatory type checking would break this compatibility and make TS migration for most projects almost impossible. Just imagine rewriting a large project from JS to Dart.

  • @nielubieyoutuba
    @nielubieyoutuba 5 місяців тому

    Java have object type. It does same thing

    • @sodiboo
      @sodiboo 5 місяців тому

      It's not quite the same thing. `any` means that the compiler won't enforce anything, and every operation is permitted. it should be used when the type *can't* be represented in typescript (i.e. never use it, unless you interop with a library that does awful things with the type system).
      `Object` means we don't know anything about this object, so everything is enforced. you cannot pass this as an argument as any other type (whereas ts `any` can be passed as any other type), and you can't *do* anything with it, except compare a few universal operations that are required on all java types. Java has a typed runtime, so it inherently can't interact with unrepresentable types, because they cannot exist (unlike in TS)
      `any` can cover all the use cases of Java's `Object`, but the inverse isn't quite true. I'm not aware of a great equivalent in Java, but the closest in C# might be `dynamic` which behaves just like TypeScript's `any`. But C# also runs on a typed runtime, so how can this be? Well, `dynamic` uses reflection to access fields on the value. of course the individual object contained is always representable, but the "real" type with semantic meaning in the context of this variable may not, i.e. if some condition outside of the type system determines whether or not a field exists at all, or if its name is likewise dependent on an external condition

  • @adtc
    @adtc 5 місяців тому

    Bad practice: Use "any" anywhere you like.
    Good practice: Add "@typescript-eslint/no-explicit-any" to ESLint rules and add an exception where "any" is really needed.

  • @h051n
    @h051n 5 місяців тому +2

    Why does Typescript exist in any?? How about that??

  • @maguilecutty
    @maguilecutty 5 місяців тому

    Cause js is a mess hahaa

  • @kilo.ironblossom
    @kilo.ironblossom 5 місяців тому

    Very neo philosophical

  • @shapelessed
    @shapelessed 5 місяців тому +2

    JS itself is too dynamic for TypeScript to be fully type-safe. It's not so rare to have a function that takes literally just any type and determines what to do with it based on some internal checks...
    For example, you might have a logger that concatenates multiple strings and numbers or extracts message and stack from errors, stringifies objects or transforms dates to your liking, then if none of the cases matched, logs the thing straight away. Strictly typing such a thing would just be a nightmare.

    • @robertsandiford6223
      @robertsandiford6223 5 місяців тому +1

      No that is perfectly possible to do in a typesafe way, even preferable, using the unknown type.

  • @charleshautly4732
    @charleshautly4732 5 місяців тому

    i dont understand a single part of this video