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
};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:
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.
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.
declare var $: any;
$( "element" ).compiler.leaves.you.alone
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.
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.
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