News for hackers. Stuff that matters.
Re:Announcing TypeScript 2.1 by ggregoire on Wednesday December 07, 2016 @01:24PM (#13124811)
> The first time you feel the speed/confidence of refactoring with accurate 'Find usages', you'll decide if the undeniable overhead of types is worth it.

For your information: in VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed.

Edit and disclaimer: Not sure why I'm getting downvoted, my comment doesn't contradict parent message. I personally type my code too (with Flow), it's an amazing tool for static code analysis and avoid errors at the runtime. I would suggest everyone give it a try (with TypeScript, Flow or whatever).

Re:Announcing TypeScript 2.1 by billti on Wednesday December 07, 2016 @03:21PM (#13125863)
If the code isn't typed, then you can't find all references accurately. For example, in the below, you've no idea if the "a" inside the function is the same is as on "foo".

  ```javascript
  var foo = {
    a: true, // Find all references on "a" here...
    b: "hello"
  };

  foo.a = false;
  bar(foo);

  function bar(obj) {
    obj.a = false; // Won't find this "a".
  }

  ```
Call site inference can follow this sometimes. However with types it can be certain, e.g.

  ```typescript
  interface Foo {
    a: boolean;
    b: string;
  };

  var foo: Foo = {
    a: true,   // Find all references on "a" here...
    b: "hello"
  };

  foo.a = false;
  bar(foo);

  function bar(obj: Foo) {
    obj.a = false; // Will be found, renamed if refactored, etc..
  }

  ```
Note that the JavaScript in VS Code is powered by the same engine as the TypeScript, so it's using the same inference, it just can't infer untyped parameters.

There is some support for JsDoc in the engine, so code like the below will work:

  ```javascript
  /** @typedef {{a: boolean, b: string}} Foo */

  /** @type {Foo} */
  var foo = {
    b: "hello"
  };

  foo.a = false; // Find all refs here
  bar(foo);

  /**
   * @param {Foo} obj - Some object
   */
  function bar(obj) {
    obj.a = false; // will find this one
  }

  ```
Full disclaimer: I work on these tools.
Re:Announcing TypeScript 2.1 by z3t4 on Wednesday December 07, 2016 @04:09PM (#13126235)
Thank you for actually providing an argument for having types. The parser could however in-line the function and replace obj for foo. I do advocate using the same variable names for function arguments though, as having different names for the same variable is not only hard to refactor, it's also very confusing! So instead of using "that", me, myself, obj, str, etc, use the actual variable name!

Disclaimer: I'm working on a competing tool

Re:Announcing TypeScript 2.1 by friendzis on Thursday December 08, 2016 @03:26AM (#13129115)
Either I do not understand your point or you are walking a thin line.

If a particular function can be tied to particular variable from scoping block, then the function is not entirely useful. On the other hand, if a function is general and has the same argument name as variable from scoping block, which is going to be passed in, mentally it is difficult to analyse the function without thinking about the variable which may shadow bugs in function. E.g. square root function may omit negative argument check, because variable from scoping block is always positive.

Instead of knowing scoping rules I advocate for distinct variable names, so scoping rules (name shadowing) do not even come into play.

Re:Announcing TypeScript 2.1 by z3t4 on Thursday December 08, 2016 @07:59AM (#13129993)
I think the problem has to do with object oriented programming and classes. Like when you have an Apple that inherits from Fruit, witch later starts looking like a Banana. With prototype you do not have that problem as you copy-paste code instead of couple code. When you rename a variable and have to search more then one function or file, then there are bigger problems with your design, because you are basically using a global variable, even though it's praised as a module. Now does your refactor tool also fix documentation and other peoples code that depends on yours ?

  function Person(name) {
    var person = this;
    person.name = name;
  }
  
  var person = new Person("Jon Doe");
  
  alert("Welcome " + person.name);

  person.name = "Harry Houdini";

  // ES6 example:
  var messages = people.map( person => person.name += " is a rock star" )
Re:Announcing TypeScript 2.1 by faceyspacey on Wednesday December 07, 2016 @09:55PM (#13128080)
Question: How close is Flow coming within VS Code to parity with the features available to TypeScript?
Re:Announcing TypeScript 2.1 by ggregoire on Wednesday December 07, 2016 @03:30PM (#13125930)
Nice catch! Yeah, that makes sense.

As you work on this, do you know if the refactoring tools would fully work with my code typed with Flow instead of TypeScript?

(I use the official flow-for-vscode extenstion, idk if it's relevant)

Edit: Ok I just tried and it works, even with the extension disabled.

  type Foo = {
    a: boolean,
    b: string,
  }

  var foo: Foo = {
      a: true, // Find all references on "a" here...
      b: "hello"
    };

  foo.a = false;
  bar(foo);

  function bar(obj: Foo) {
    obj.a = false; // Found it.
  }
Re:Announcing TypeScript 2.1 by billti on Wednesday December 07, 2016 @03:41PM (#13126016)
I don't know, but generally call-site inference has a couple of notable issues:

- If you don't have any calls to the function yet (i.e. "bar(foo)" wasn't there above), then there is nothing to infer from, so you have no idea what the type is at this point (other than by the use of the parameter inside the function).

- Is your call-site wrong? If the parameter type is explicit, this is easily checked. If not, then again you can only go by what the function does with it. (Which may not be much help if it's something like "RestAPI.post(JSON.stringify(theParamInQuestion))" ).

Re:Announcing TypeScript 2.1 by johnfn on Wednesday December 07, 2016 @02:12PM (#13125261)
> For your information: in VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed.

I didn't downvote, but this seems incorrect to me. I'm sure that it works in small code bases, but once your code gets large enough (and dynamic enough) it's going to have to start missing things.

Re:Announcing TypeScript 2.1 by ggregoire on Wednesday December 07, 2016 @02:26PM (#13125386)
Why would their algorithm stop to work if your code gets large? It's not some kind of magic. :)

My current project has 10,000 LOC and 130 files. If I use "Rename symbol" on something imported in different files of my project, the editor will open all the files, edit all the names and just wait for me to save the changes.

Re:Announcing TypeScript 2.1 by 13years on Wednesday December 07, 2016 @02:33PM (#13125447)
Doesn't work at all for me. My project has about 500k+ LOC. I suspect though it isn't the size of project, but certain patterns of design that work or don't work in non typed JS.

However Brackets does better at finding method definitions. Even in my mess of a project with non typed JS, Brackets can always find the method definition or at least give me possible choices.

Re:Announcing TypeScript 2.1 by johnfn on Thursday December 08, 2016 @01:40AM (#13128828)
> Why would their algorithm stop to work if your code gets large? It's not some kind of magic. :)

Their algorithm always fails on certain cases. If your code base is larger, it's more likely to contain those cases.

Example case that will always fail:

    const x = { foo: () => {} }
    x["f" + "oo"]();
Now rename foo to bar.

Yes, this is a contrived case, but we had similar code in production at my last job.

Re:Announcing TypeScript 2.1 by stcredzero on Wednesday December 07, 2016 @02:08PM (#13125224)
VSCode, the "Find all references" and "Rename symbol" work out-of-the-box, even if your code isn't typed.

With good coding practice, references, "senders" and "implementers," and renaming worked well in Smalltalk. Where I missed types was during refactoring, and that also worked well about 99% of the time without types. It was that one last thing that we couldn't be 100% sure of that blocked the big refactoring we all wanted to do badly -- that's when I missed types!

Re:Announcing TypeScript 2.1 by xiaoma on Thursday December 08, 2016 @12:03AM (#13128511)
VSCode has become a mind-blowingly good editor. It's closing in on the power of heavy-weight IDEs with much better performance than some code editors (e.g. Atom), even.
Re:Announcing TypeScript 2.1 by dotancohen on Thursday December 08, 2016 @03:12AM (#13129077)
Both VSCode and Atom are based on Electron, which is slow and battery-intensive as it is based on NodeJS. Are you saying that recent VSCode versions are as performant as, say, a C++ based editor or even (shudder) a Java based editor such as Jetbrains (Webstorm, etc.)?

I last looked as VSCode (on a KDE 4 CentOS 7 desktop) about half a year ago, and it was terribly slow, even without extensions.

Re:Announcing TypeScript 2.1 by tomduncalf on Thursday December 08, 2016 @10:57AM (#13131101)
I would say VS Code is far more performant than the Jetbrains editors in my experience on a 2013 MBP. It may be that the Jetbrains editors are "doing more", but I always found them frustratingly sluggish - maybe a worthwhile trade off when using something like Scala but I personally couldn't deal with it for Javascript.

I'd really recommend trying it again. On a performant machine, VS Code feels like a native app to me aside from a slightly slower start up time, whereas Atom is noticeably slow. On my bottom spec 12" Macbook the difference is more noticeable, but everything feels slow on there.

Re:Announcing TypeScript 2.1 by xiaoma on Friday December 09, 2016 @05:43AM (#13137939)
VSCode runs circles around Atom and Jetbrains both.