<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Sainati on Medium]]></title>
        <description><![CDATA[Stories by Daniel Sainati on Medium]]></description>
        <link>https://medium.com/@danielsainati?source=rss-36d05ccb715a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BO8wurklk52vy4lK</url>
            <title>Stories by Daniel Sainati on Medium</title>
            <link>https://medium.com/@danielsainati?source=rss-36d05ccb715a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:30:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielsainati/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sound Typing for “this” in Flow]]></title>
            <link>https://medium.com/flow-type/sound-typing-for-this-in-flow-d62db2af969e?source=rss-36d05ccb715a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d62db2af969e</guid>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[javascript-types]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[flowtype]]></category>
            <dc:creator><![CDATA[Daniel Sainati]]></dc:creator>
            <pubDate>Wed, 02 Jun 2021 17:27:06 GMT</pubDate>
            <atom:updated>2021-06-02T17:27:06.292Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/flow-type/clarity-on-flows-direction-and-open-source-engagement-e721a4eb4d8b">In our most recent post</a>, we alluded to some upcoming changes that would restrict some common JavaScript features. In this post, we will outline the first few of these changes, which you can expect to see in upcoming releases, and which are designed to address one of Flow’s longest-standing unsoundnesses: typing the this parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HNZ2tmvcz4K8iwfiavStwA.jpeg" /><figcaption>Photo credit to <a href="https://www.pexels.com/photo/binding-books-color-colorful-273034/">Pixabay</a> from <a href="https://www.pexels.com/">Pexels</a></figcaption></figure><p>The this parameter of functions and methods has historically been one of Flow&#39;s major blind spots; we were able to infer a type for this when functions were used within a file, but had to resort to inferring any for any exported functions. Class methods assumed that methods were called on an instance of the class, but this was not enforced. So the following code would not generate any errors in Flow.</p><pre>class C {<br>  foo() {}<br>  bar() { this.foo() }<br>}<br>var bar = new C().bar;<br>bar(); // runtime error!</pre><p>Similarly, in the below code, Flow would allow you to call the exported function from other files with any this argument, and you were unable to provide an annotation for the parameter.</p><pre>export function foo() : number {<br>  return this.bar;<br>}</pre><p>To address these issues, Flow is adding support for annotating this parameters of functions and methods, as well as restricting and modifying the way that this interacts with some features of classes, class methods, and objects. To provide sound and simple behavior for this new feature, Flow will error when class methods are unbound and when this is used inside object methods. Flow is also restricting the ways that class instances interact with object types. The behavior of the this parameter is commonly a source of confusion, and JavaScript itself has evolved away from using it by introducing new language features like arrow functions and classes that discourage its use. By eliminating outdated, complex language features and instead focusing on modeling modern idioms, we can make Flow a simpler, safer and friendlier language. <br> <br>However, this is a large set of changes that modify some pretty important parts of Flow’s type system, so updating your codebase to be compatible with these improvements may be challenging, and may take some time. We have released some tools (which will be detailed below) that will help with these improvements, but we encourage upgrading to the newest version of Flow and suppressing (and then fixing over time) the new errors, rather than attempting to fix all the errors ahead of time. That all said, we think the benefits are very worth it, so let’s dive into the specifics:</p><h4>&quot;this” parameter annotations</h4><p>Inspired by TypeScript, we will be adding support for this annotations in a special position at the beginning of a function&#39;s parameter list. When present, the this parameter must always have an annotation, and must always appear first in the list. The following are valid examples of this annotations:</p><pre>function foo(this: { x: string }) {<br>  /* code */<br>}</pre><pre>interface I {<br>  m(this: I): void;<br>}</pre><pre>class A {<br>  m(this: I): void {<br>    /* code */<br>  }<br>}</pre><p>while the following are not:</p><pre>function bar(x: number, this: string) {<br>  /* code */<br>}<br>function baz(this, ...args) {<br>  /* code */<br>}<br>let x = (this: number) =&gt; {<br>  /* code */<br>};</pre><p>A couple additional points about this parameters to be aware of:</p><ul><li>As you can see in the above example, arrow functions cannot have this parameters, as they bind this at the definition site, rather than the call site. This would make a this parameter useless on such a definition.</li><li>Functions and methods now check their this argument against their annotated (or inferred) this parameter at call time. So, using the example above, foo.bind({x: 3})() would error on an incompatibility between the value of 3 and its expected string type.</li><li>Classes will default to inferring the instance type for the this parameter of instance methods, and the class type for the this parameter of static methods, unless otherwise annotated. Declared classes and interfaces will default to mixed.</li><li>Class and interface methods will require that their this parameter is a supertype of the type of the interface or class, as this is required for subtyping to be sound. Annotating a class method with a specific this parameter will affect the type of this inside the method, potentially restricting the properties or methods that can be accessed in the body.</li><li>As a result of these changes, using Flow after this change will require updating to a version of Babel that has support for this new syntax: at least version 7.13.0, since in order to resolve incompatibilities between this parameters, you may need to explicitly annotate them. Additionally, in future versions of Flow (more on this in another upcoming post), we will be requiring explicit this parameter annotations on top-level functions using this in the body.</li></ul><p>This has a number of benefits: it allows Flow to make stronger guarantees about the this parameters of methods, as well as type patterns we previously could not support. For example, in the past we loosely typed Array methods like map using mixed for the callback’s thisArg, which did not actually check against the callback properly. Now instead of writing the method definition for map like this:</p><pre>map&lt;U&gt;(callbackfn: (value: T, index: number, array: Array&lt;T&gt;) =&gt; U, thisArg?: mixed): Array&lt;U&gt;</pre><p>we can instead type map more safely as:</p><pre>map&lt;U, This&gt;(callbackfn: (this : This, value: T, index: number, array: Array&lt;T&gt;) =&gt; U, thisArg: This): Array&lt;U&gt;</pre><h4>Restrictions on method unbinding</h4><p>Consider the following code sample in which a class method is rebound, resulting in a runtime error:</p><pre>class B {<br>  m() {}<br>}</pre><pre>class C extends B {<br>  bar = () =&gt; null;<br>  m() {<br>    this.bar();<br>  }<br>}</pre><pre>var x: B = new C();<br>x.m.apply(new B()); // rebinding leads to a runtime error!</pre><p>This will error at runtime, as B does not have a defined bar property, but x.m requires bar to exist on its this argument. One might wonder what this parameter we might infer here that would detect this error, but it turns out that there is in fact no practical sound way to prevent this type of runtime error from occurring without banning subclassing outright. In order to ensure that this parameter annotations are sound, starting with version 0.153.0 Flow will ban the unbinding of class methods from the context in which they were defined. <br> <br>This means that all of the following will error:</p><pre>class A {<br>  m() {}<br>}</pre><pre>let a = new A();</pre><pre>// error: explicitly unbinding m from a<br>let b = a.m;</pre><pre>// error: bind, apply and call cannot be used with class methods<br>a.m.bind({}); </pre><pre>// error: destructuring implicitly unbinds m from a<br>let { m } = a; </pre><p>Flow’s LSP interface supplies quickfixes for these new errors, so if you run into them while writing code, you will be able to apply quickfixes to resolve them automatically. Additionally, in order to facilitate upgrading your codebase past this change, we are introducing a new command to Flow’s CLI interface: fix. Running flow fix --write in your project will modify your code by applying the quickfixes for these new errors in all the places they show up. In particular, the fix command will modify unbound methods to become arrow method properties. In the above example, the class A would be rewritten as:</p><pre>class A {<br>  m = () =&gt; {}<br>}</pre><p>It is worth noting that this may introduce variance errors (since methods are covariant) that may need to be fixed, and is not entirely safe if you were explicitly rebinding these class methods or accessing them directly from the class prototype. In particular, you may need to ensure that class hierarchies have the same type of definition for matching fields. I.e., if m is a method on A, it should be a method on all superclasses and subclasses of A. The same principle holds for function or arrow properties. In cases where methods cannot be easily transformed into arrows, we suggest suppressing the unbinding errors to ease the version upgrade.</p><h4>Restrictions on this in objects</h4><p>Flow will be banning the use of this inside of object methods. We recommend assigning the object to a variable and referencing that to achieve the same kind of behavior. For example, you could rewrite this code:</p><pre>let o = {<br>  x: &quot;&quot;,<br>  write(s: string) {<br>    this.x = s;<br>  },<br>  read() {<br>    return this.x;<br>  },<br>};</pre><p>into:</p><pre>let o = {<br>  x: &quot;&quot;,<br>  write(s: string) {<br>    o.x = s;<br>  },<br>  read() {<br>    return o.x;<br>  },<br>};</pre><p>It is difficult to automate this change, so we suggest that in order to upgrade past this change, you suppress the new object-this-reference errors that appear and fix instances of this as you encounter them in your code.</p><h4>Changes to class-object subtyping</h4><p>The final change in this set reorganizes the type hierarchy for objects and classes. Currently in Flow, an inexact object that defines the same fields and methods as a class is a valid supertype of an instance of that class. In version 0.153.0 of Flow, we are changing this behavior: objects will no longer be valid supertypes of class instances; instead, interfaces can be used to achieve the same effect. So code like the following:</p><pre>function foo(x: { p: number, ... }) {<br>  /* code */<br>}<br>class A {<br>  p: number;<br>}<br>foo(new A()); // class-object-subtyping error</pre><p>can be rewritten like this:</p><pre>function foo(x: interface { p: number }) {<br>  /* code */<br>}<br>class A {<br>  p: number;<br>}<br>foo(new A());</pre><p>This restriction was put in place to make the previously mentioned changes safe; since we are adding differing behavior to classes and objects, it no longer makes sense to be able to cast between them. Object types will now be distinct from classes, while interfaces will express the supertype of both structures. Conceptually, you can think of an interface as a type describing the accessible properties and methods on a value, whether it’s a number, a class or an object. Interfaces are similar to inexact objects in their subtyping behavior, so all the following are valid uses of interfaces:</p><pre>let a: interface { length: number } = &quot;&quot;;</pre><pre>let b: interface { toUpperCase(): string } = &quot;&quot;;</pre><pre>let c: interface { toFixed(fractionDigits?: number): string } = 3;</pre><pre>let d: interface { x: string } = { x: &quot;&quot;, y: &quot;&quot; };</pre><pre>let e: interface { x: string } = class {<br>  static x = &quot;&quot;;<br>  static y = &quot;&quot;;<br>};</pre><pre>let f: interface { x: string } = new (class {<br>  x = &quot;&quot;;<br>  y = &quot;&quot;;<br>})();</pre><p>However, interfaces will also have the same restrictions as classes on unbinding their methods.</p><p>In order to facilitate upgrading your codebase past this change, the fix command mentioned above will include an autofix that automatically refactors object types to interfaces when they appear as supertypes of classes (the example above with the function foo demonstrates what this change will look like). As was the case previously, this autofix will also be applicable directly from the IDE when you encounter this error.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d62db2af969e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flow-type/sound-typing-for-this-in-flow-d62db2af969e">Sound Typing for “this” in Flow</a> was originally published in <a href="https://medium.com/flow-type">Flow</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Flow error suppressions more specific]]></title>
            <link>https://medium.com/flow-type/making-flow-error-suppressions-more-specific-280aa4e3c95c?source=rss-36d05ccb715a------2</link>
            <guid isPermaLink="false">https://medium.com/p/280aa4e3c95c</guid>
            <dc:creator><![CDATA[Daniel Sainati]]></dc:creator>
            <pubDate>Mon, 16 Mar 2020 19:28:43 GMT</pubDate>
            <atom:updated>2020-03-16T19:28:43.216Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BDz7w3FNjQHVFEuY5pDjJg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@freeimages9?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">icon0.com</a> from <a href="https://www.pexels.com/photo/assortment-black-and-white-black-and-white-chrome-220639/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><h3><strong>We’re improving Flow error suppressions so that they don’t accidentally hide errors.</strong></h3><p>Over the coming year, Flow is planning to make <a href="https://medium.com/flow-type/what-were-building-in-2020-bcb92f620c75">many changes and improvements to its type system</a>. One of the consequences of this, however, is that you may need to add more suppressions to your code as Flow gets better at finding existing issues.</p><p>This can be a problem because Flow’s suppressions affect more than just the original error you intended to suppress. This reduces Flow’s ability to catch errors. This is a result of two properties: First, that <strong>Flow suppressions can be located at any part of a type error,</strong> and second, that <strong>Flow suppressions can suppress any kind of type error</strong>. To fix these shortcomings, we are going to require that suppressions be placed at the location in your code where the error actually occurs, and that they include an error code specifying the kind of error you want to suppress.<br> <br>To understand what all this means, let’s dig in to the nature of Flow’s type errors.</p><h3>What is a type error?</h3><p>When Flow encounters an issue while checking your code, like an incompatibility between two types or an invalid property access, it generates an error that reports to the user precisely what is wrong with their code. These errors can contain a wide variety of information depending on the particular circumstances that caused them, but the two that we care most about are the <strong>kind</strong> of the error and the <strong>locations</strong> of the error.<br> <br>The <strong>kind</strong> of the error encodes the specific nature of the bad behavior that Flow detected in your code, whether it be something simple like passing too many arguments to a function, or something complex like spreading a union type. This information tells Flow exactly what kind of information to display to the user to best explain exactly where you went wrong. <br> <br>The <strong>locations</strong> of the error contain information about where the error is located in your code. There are two kinds of locations an error can contain, a <strong>primary</strong> location (of which there can be only one), and <strong>secondary</strong> locations, of which there can be many or none at all. The primary location roughly corresponds to the location in your code where the error actually occurred, while the secondary locations carry information about the definition sites of the types and values involved in the error. For example:</p><pre>function foo(x : number) : void {}<br>foo(&quot;hello&quot;);</pre><pre>/* error */<br>foo(&quot;hello&quot;);<br>       ^ Cannot call `foo` with `&quot;hello&quot;` bound to `x` because string [1] is incompatible with number [2].<br>References:<br>2: foo(&quot;hello&quot;);<br>       ^ [1]<br>1: function foo(x : number) : void {}<br>                    ^ [2]</pre><p>In this example, the error occurs when foo is called with &quot;hello&quot;, since &quot;hello&quot; is not a number. This means that the primary location of the error, where it occurs, is on the second line. The secondary location of this error is on the first line, because foo is a part of this error, and the first line is where it is defined. If we were to call foo with a variable, however, like this:</p><pre>function foo(x : number) : void {}<br>let y : string = &quot;hello&quot;;<br>foo(y);</pre><pre>/* error */<br>foo(y);<br>       ^ Cannot call `foo` with `y` bound to `x` because string [1] is incompatible with number [2].<br>References:<br>2: let y : string = &quot;hello&quot;;<br>           ^ [1]<br>1: function foo(x : number) : void {}<br>                    ^ [2]</pre><p>The error here has its primary location on the third line, but has two secondary locations at the definition sites of foo and y.</p><h3>So what’s so wrong with suppressions?</h3><p>Well, because suppressions can be applied on both the primary and secondary locations, putting a //$FlowFixMe comment above the definition of foo would suppress the errors in both of the above examples. Since foo appears as a secondary location in every error that uses it, that would mean that any use of foo that results in an error would be suppressed by this one suppression, anywhere that it might occur. You would be able to call foo with any number of arguments of any type and Flow would not surface any errors or warnings to you about any of this. This degrades the confidence of users in Flow’s type checking and allows bugs it would otherwise have caught to make it into production.</p><p>In addition to this, Flow’s suppressions affect all kinds of errors, as long as they contain a location that the suppression covers. This means that multiple errors of different kinds can be suppressed by a single suppression. To see the danger in this, consider the following example:</p><pre>// library file lib.js</pre><pre>function foo(x : number) : void {}</pre><pre>// impl file<br>const {foo} = require(&quot;lib.js&quot;);<br>let y : string = &quot;hello&quot;;<br>// $FlowFixMe<br>foo(y);</pre><p>This suppression comment prevents Flow from raising the type incompatibility issue here, but let’s imagine you’re okay with this because it works at runtime. Now imagine that an update to the library file added a second argument to foo, and uses it in such a way that not providing this argument would cause a runtime error. Now there is a second error on the call to foo: the call does not supply the right number of arguments, but Flow will not report this to you, because there is still a suppression on the previous line. Our change has caused Flow to raise a second type of error, but the suppression captures both of them, and the bug is able to escape Flow’s notice.</p><h3>What are we going to do about this?</h3><p>To address this we are going to make the following changes:</p><ul><li><strong>Enforce primary locations.</strong> We will be changing the suppression behavior such that suppressions only apply to errors when placed at their primary location. For example, instead of allowing you to suppress an invalid call at the definition of the function, we will now require that the suppression be located at the call-site itself. This way, Flow will still be able to alert you about other possibly invalid calls to the function.</li><li><strong>Add error codes.</strong> We will also be adding error codes to our suppressions so they only suppress errors of the specified kind. This will prevent suppressions from unexpectedly suppressing a kind of error you were not expecting to encounter.</li><li><strong>Standardize suppression syntax.</strong> Previously the acceptable syntax for an error suppression could be configured manually in the .flowconfig for a given project, allowing inconsistent suppression syntax across projects. As part of the above changes we will also be standardizing the suppression syntax; only supporting the $FlowFixMe[incompatible-type] or $FlowExpectedError[incompatible-type] formats.</li></ul><p>Note that the enforcement of primary locations will be released in version 0.121.0, while error codes and standardized suppressions will be added in a later release.</p><h3>Rollout</h3><p>We recognize that this change may cause a significant number of errors in your codebases to become invalid, especially if you were in the habit of placing suppressions in library code. To ease this burden, we have a couple of suggestions:</p><ul><li>To relocate your newly invalid suppressions to their primary locations, we recommend a combination of the add-comments and remove-comments utilities provided in the Flow tool. Running ./tool remove-comments will remove any comments that no longer suppress an error because they are not on a primary location, and ./tool add-comments will place new suppressions on unsuppressed locations. The ./tool script can be accessed by cloning the Flow repository on GitHub.</li><li>Suppressions without error codes will continue to suppress any and all errors on their location, but once we roll out the error codes feature, you can add the proper codes to your codebase via a similar process as above. Removing all your old comments and re-adding them with add-comments will include the error code in the newly added comment.</li></ul><p>We look forward to bringing you a safer codebase!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=280aa4e3c95c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flow-type/making-flow-error-suppressions-more-specific-280aa4e3c95c">Making Flow error suppressions more specific</a> was originally published in <a href="https://medium.com/flow-type">Flow</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>