4 ms·
The TypeScript team has said that if they could do it over, `any` would behave like `unknown`.
by spankalee 4y ago
The TypeScript team has said that if they could do it over, `any` would behave like `unknown`.
- Kiro 4y agoThat sounds extremely cumbersome. Sometimes I just want to throw an any on something and call a method on it without having to deal with types. How would unknown help with that? I can't call a method on a variable with unknown type.
- haburka 4y agoYou can use ‘as’ to cast unknown to any type. This is extra typing but it does signify to other developers that you’re calling a function on an unverified object, which I believe makes the code more maintainable in the long run.
- anentropic 4y agoWhy bother using typescript then?
- Kiro 4y agoThat 1% won't invalidate the remaining 99%.
- golergka 4y agoThis is a good thing for serious, long lasting projects. However, it could be an overkill for something fast and dirty. One thing I love about typescript that I've never seen in other languages is that you can adjust the type checker exactly for the level of rigidness that project at hand needs. Any is okay for 200 LOC quick prototypes.
- Gigachad 4y agoThis is a bit of a problem imo. It’s super easy to forget to add a type somewhere and then it just defaults to any. I’d prefer no implicit any so you have to knowingly set something as untyped. It’s also just flat out impossible in many other languages because the compiler actually needs to know the types for performance reasons.
- gchq-7703 4y agoThis is possible with a single `tsconfig.json` change[0]. This is also the default mode if you've set the 'strict' flag. [0] https://www.typescriptlang.org/tsconfig#noImplicitAny https://www.typescriptlang.org/tsconfig#noImplicitAny
- mablopoule 4y agoYes, exactly. Typescript managed to be the best of both worlds, as you can be both lenient when you're still in the design/prototyping phase of a project, and need to test idea quickly, but also add as much type-check as you see fit when you're maintaining a project, and need to interface with lots of existing code. As such, I'm always a bit miffed when I see so many people adamant on setting it to maximum strictness by default, under the guise of "best practice".
- moogly 4y agoAs someone who has "strictified" a lot of code bases after the fact, starting out with `strict` is way less friction than adding it later on. Especially `strictNullChecks`. It's a massive headache to deal with that later on.
- eyelidlessness 4y agoYup. And strict mode tends to guide devs towards better code/API design. Stuff like function/method overloads with dynamic argument types might seem convenient when the types are loosey goosey, but it usually becomes obvious they’re probably a bad idea when you have to specify the types upfront.
- capableweb 4y ago> Any is okay for 200 LOC quick prototypes If you're writing a prototype (so the code will eventually be thrown away), it's just 200 LOC and you're using `any` for everything, why even use TypeScript? Should just use vanilla JavaScript instead. And if you wanna break the rule of throwing away prototype code, you can always "upgrade" it to TypeScript later without too much fuzz.
- solarkraft 4y agoStandard tooling. It's also still useful even if you just consume the types.
- Klathmon 4y agoWho said using any for everything? I love using TS for prototypes because you can define the interfaces wherever you feel they're important, and leave the rest to any. And like you said, you can always improve the types later
- nicoburns 4y ago> If you're writing a prototype (so the code will eventually be thrown away), it's just 200 LOC and you're using `any` for everything, why even use TypeScript? Should just use vanilla JavaScript instead. The case where I've found this useful is making a quick and dirty prototype that integrates within an existing typescript codebase. Such a prototype would never make it to the main branch but sometimes it's useful to make a quick proof of concept of the the runtime behaviour before comitting to sorting out the types.
- bazoom42 4y ago‘any’ is great for porting javascript into typescript, and integrating with javascript libraries. You can add types gradually, without everything grinding to a halt.
- schwartzworld 4y agoAdding types to code that was written with `any` is going to be much harder than using `unknown`.
- bazoom42 4y agoThe point is if you have existing JavaScript you want to integrate with TypeScript. You should not need ‘any’ in code written in TypeScript from the beginning.
- schwartzworld 4y agoSure, but at least with unknown you will have done some narrowing. Writing Typescript well requires some fundamentally different patterns than javascript. Adding types afterward is like pulling out your teeth slowly.