Hacker News new | past | comments | ask | show | jobs | submit login
TypeScript 5.2 (microsoft.com)
47 points by styfle 8 months ago | hide | past | favorite | 12 comments



Am I missing the point or is the “using” thing just a very over-engineered destructor that you can FORGET to call? It’s nothing like Python’s “with” that lets you define a scope for an object to be destructed early. If you forget, or simply don’t know that the object supports “disposal”, will the object simply never be disposed? Or will the TSC not compile if you leave out a “using” for a disposable object?


> It’s nothing like Python’s “with” that lets you define a scope for an object to be destructed early.

You can forget to use `with` as well. Destructors in python are called when the reference count hits zero or during a gc pass. These new typescript destructors apply to the variable, not the object, so they run when the variable goes out of scope, regardless of what other references there might be to the same object. If you want to emulate `with` more closely, you can create a new scope for your `using` declaration, and put it at the very top of that new scope.


TSC should fail the build if it doesn't have the destruction interface, the scope is the scope the variable is initialized in/under. It's very similar to C#'s usage of using.

Edit: It's also a pending Stage 3 proposal for EcmaScript.

https://github.com/tc39/proposal-explicit-resource-managemen...


I think they’re worried about the opposite, forgetting to put using when you needed it


Given my experience with TS, would assume the opposite is also true. Will likely lint for manual call or for using.


> Will likely lint for manual call or for using.

That's a safe bet.

TypeScript's ability to detect problems in the code seems superior than what Visual Studio ships with for C# - for example it doesn't warn about forgetting to `await` a method call.


But if the opposite was true, why do I have to specify it at all? Why can’t it free on scope exit like any other language, with “let”?


Because it will run in JS and you can do stuff like return something with a lifetime to be scoped at a higher level. Is there too declare intent. Typescript is just really advanced linting.


Coming from C# I made it a habit to always check when I don't know a class.


The "If you’re not familiar with TypeScript" paragraph is excellent. Good job, author!


Array.prototype.with looks rad!


I really don't like using the name "with". It's pretty confusing IMO. Plus, you can't destructure it because it's a keyword.

With Array.prototype.with, it's now array.with(0, 3), which isn't exactly the most descriptive signature in the world.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: