News for hackers. Stuff that matters.
Re:TypeScript support in Electron by TheAceOfHearts on Friday June 02, 2017 @04:20PM (#14472992)
I prefer Flowtype, but I definitely agree that TypeScript seems to be winning on the popularity front.

One of the big problems is that flow has far fewer definition files available. Most projects don't want to support two separate type specs. Even if it didn't provide 100% compatibility, having something take you 90% of the way there would be a big improvement.

But I'm still betting on flow. I like that it's supported by babel and it generally seems to be better at helping you catch bugs. @thejameskyle recently tweeted [0] a short snippet showing a great example showcasing flow.

[0] https://twitter.com/thejameskyle/status/868148120504356864

Re:TypeScript support in Electron by orif on Friday June 02, 2017 @05:43PM (#14473539)
TypeScript would complain about that snippet too.

function getOptions() { return { num: 42, str: "val" }; }

var __options__ = getOptions();

type Options = typeof __options__;

var options: Options = {

    ^^^^ Type is not assignable

    num: "hi",
    str: 42
};
Re:TypeScript support in Electron by silviogutierrez on Friday June 02, 2017 @07:10PM (#14473918)
I love TS and cannot speak enough good things about it.

But in your TS example, the code would execute. Not in the Flow example, where it's just an annotation.

There are hacks and other approaches, documented here:

https://github.com/Microsoft/TypeScript/issues/6606

Re:TypeScript support in Electron by Arnavion on Saturday June 03, 2017 @12:35AM (#14475027)
>But in your TS example, the code would execute.

Incorrect. It's a type alias so it uses "type typeof" not "value typeof". It doesn't have the problem that the issue you linked to is about.

https://www.typescriptlang.org/play/#src=function%20getOptio...

Observe the JS output contains no `typeof` and that the type `Options` is considered to be the same as `{ num: number, str: string }` if you hover over it.

Re:TypeScript support in Electron by silviogutierrez on Saturday June 03, 2017 @02:22AM (#14475264)
I don't understand. In your transpiled code, getOptions is clearly being called in JS.

In flow, getOptions() wouldn't be in the transpiled code at all.

Imagine the body of getOptions as an extremely expensive computation. Some prime calculation. But you only want its return type. In flow comments, you can get it.

In the approach of your snippet, you could get it, but it will execute the full function with the full price.

Re:TypeScript support in Electron by Arnavion on Saturday June 03, 2017 @03:34AM (#14475423)
My bad, I thought you were referring to `typeof` being the code that still executes. Now I read that tweet more carefully and noticed the `getOptions()` call is in a comment. A strange feature that I suppose is useful when dealing with complex anonymous types, though in my experience such complex types tend to be named.
Re:TypeScript support in Electron by wereHamster on Saturday June 03, 2017 @04:19AM (#14475508)
The minifier would get rid of the __options__, and also the function getOptions because both are not used in the resulting code. You need a minifier in any serious project.
Re:TypeScript support in Electron by silviogutierrez on Sunday June 04, 2017 @03:12AM (#14480249)
It wouldn't. getOptions() could have side effects that we want to run. Imagine inside of getOptions we have a prompt() call to fill in some of the options...
Re:TypeScript support in Electron by AgentME on Friday June 02, 2017 @05:13PM (#14473346)
Does Typescript still require you to have definitions for every library in order to use the library? One thing I love about Flow is that it doesn't have that requirement. You can start using a new library, and later on if you find that type safety in that library's exports would be useful, then you can look for or write definitions then. You're not blocked from adopting the library. I find for many of my dependencies that I use in a few spots it's not really important for me to have definitions for.
Re:TypeScript support in Electron by jsight on Friday June 02, 2017 @05:28PM (#14473448)
I don't think that Typescript ever did require that? AFAIK, it has always been possible to just declare the variable as "any" type and use it as if it were untyped.
Re:TypeScript support in Electron by AgentME on Saturday June 03, 2017 @01:11AM (#14475098)
I thought I remembered using require statements and getting errors from the typescript compiler that it couldn't find the (typescript definitions for the) file.
Re:TypeScript support in Electron by neilsimp1 on Friday June 02, 2017 @05:54PM (#14473600)
Can confirm. This has been my experience.
Re:TypeScript support in Electron by typebore on Friday June 02, 2017 @05:26PM (#14473439)
technically you do need to define at the top level, but no, not in a practical sense:

declare var $: any;

$( "element" ).compiler.leaves.you.alone

Re:TypeScript support in Electron by boubiyeah on Friday June 02, 2017 @05:32PM (#14473476)
You can always add a quick and dirty definition like:

declare module 'themodule' { var mock: any; export default mock; }

But unless the lib doesn't propagate through your code at all, it pays of to give it proper typings.

Re:TypeScript support in Electron by mattmanser on Friday June 02, 2017 @08:28PM (#14474213)
In the rare cases we find a library that doesn't have a typescript definition, we just do:

   declare var Thing : any;
In a global definition file.

Pro tip. We've got a lot of mixed js and ts at the moment and when you're really lazy you can take advantage of how every object is a dictionary in js and just do something like `window["notification"]` instead of window.notification, which resolves to an 'any' type in ts.

Obviously you should try and minimise that usage, but it's a useful trick to know.

Re:TypeScript support in Electron by mercer on Sunday June 04, 2017 @09:00AM (#14481331)
Is that still the case with the recent update to TypeScript now that it also typechecks object['prop']?