Home > News content

Google employees complain that TypeScript: type checking is not good

via:博客园     time:2019/9/12 15:26:26     readed:141

Recently, the Google employee named Evan Martin published a report on TypeScript in the GitHub repo of TypeScript.


thoughTypeScript 3.5 releaseIt has been three months (the latest stable version 3.6 was released at the end of last month), but the Google development team has only recently upgraded to version 3.5. After a period of use, developers feel that they do not vomit, so there is a high quality feedback on the use of this article. Yes, the project mentioned here is the Google that is used by people.


The project facing the development team is Google, which has billions of lines of code. Within the team, all members use the same version of TypeScript and the same set of compiler flags across all platforms. If upgraded, members will help upgrade these flags for all at the same time.

Evan said that he, like everyone else, would expect some improvements from the new version of TypeScript. For example, Evan said he wanted and welcomed improvements to standard libraries, even if that might mean removing similar but incompatible definitions from code libraries. However, the team found that the upgrade to TypeScript 3.5 resulted in much more additional work than previous upgrades.

Evan believes that three major changes in version 3.5 make this upgrade particularly difficult. He believes that most of these changes are aimed at improving type checking, but he also believes that type checking as understood by the TypeScript team is always a trade-off between safety and efficiency.

To this end, Evan hopes that this large code base-based TypeScript will use feedback to help the TypeScript team better assess similar situations in the future and provide some suggestions.

Let's look at three major changes that Evan says will affect the team in Version 3.5.

Implicit default for generics

This feature is a devastating change in version 3.5, and Evan believes that the reason for the problem here is that the generics of the code are not relevant to what the code does. For example, suppose there is some code with Promise parsing, but it doesn't care about the value that Promise parses:

function dontCarePromise () {  return new Promise ((resolve) =

Because generics are unbound, in 3.4In 3.5, the code becomes。 If the user of this function writes this type of Promise anywhere:

const myPromise: Promise

This will result in type errors.

in addition, there is also a method known as that

Expects String (myFunction ());
It can be reconstructed legally in the following ways:

Const x = myFunction ();

But in the end, it was not feasible.

Boolean filter

TypeScript 3.5 changedBooleanType of function, which forces assignment toBooleanFrom

Function Boolean (value?: any): boolean;

Turn into

function Boolean

The two may look very similar. But imagine a function that accepts a predicate and returns an array filter and uses it as the code above:

function filte

In version 3.4, by definition,TfromAnyTurn intoMyFilterAnd become a byAny[]reachAny[]Function. But in version 3.5,TOnly generics are retained.


In TypeScript 3.4, the following code:

Const s = new Set ();

Will return one。 However, a change has occurred in TypeScript 3.5 to enableLib. es2015. iterable. D. TSWith removalAnyThe effect then causes the generic to change the description above and deduce the type asUnknown

This change is ultimately difficult to fix, because the final type error sometimes falls far short of the actual problem. For example, in the following code:

class C {  gather () {    let s = new Set ();    s.add ('hello');    return s;  }  use (s: string[]) {

We will receive the information about it.Array.fromType error prompts, but what actually needs to be fixed isNew Set ()


Evan said they were very satisfied with TypeScript, and the feedback was only intended to provide some reference for the team in designing new features to better develop TypeScript.

China IT News APP

Download China IT News APP

Please rate this news

The average score will be displayed after you score.

Post comment

Do not see clearly? Click for a new code.

User comments