Hacker News new | ask | show | jobs
by aphexairlines 1096 days ago
This is a JavaScript keyword and there's nothing TypeScript-specific in the blog post.
3 comments

I get you, but it's both a TypeScript and JavaScript feature, and more importantly TypeScript is including it in its next official release whereas it is still not part of ECMAScript (though close, at stage 3).

So speaking specifically of a new TypeScript feature is justified here.

typescript is introducing it in a polyfill, which is going to be how a lot of people will first get to use the feature
How does the polyfill work? How does TS know the object can be disposed?
You're telling it through the `using` statement?
I mean the implementation of the polyfill, in JS
That has the same answer. I'm not sure what you're looking for.

The polyfill adds code at the start of the block to make an array, and code at the end of the block to call dispose on everything in the array. Then the transpiler just has to turn "using" into an append.

There's some other details to handle exceptions but that's the basics of how it works.

Edited: The code, polyfill or native, doesn't really know if things can be disposed. It just tries to do it, because you told it to. If the variable is null it skips it, otherwise it asserts that the dispose function exists at using time, and blindly runs it at the end of block.

Thanks, that's exactly what I was wondering.

What happens if you save a reference to (or return, etc.) the variable you declared with "using"? Then it will end up automatically disposed, but from the programmers point of view it should still be usable (not disposed).

The article doesn't hide that, and there is something TS-specific: the information that it (that JavaScript version) will come in v5.2.

I think it's fair enough if you're coming from a TS perspective, writing TS (specifically) or blogging about it. Obviously there's overlap.