TypeScript is a primary language for Angular application development.
It is a superset of JavaScript with design-time support for type safety and tooling.
This page covers some aspects of TypeScript configuration and the TypeScript environment
that are important to Angular developers, including details about the following files:
TypeScript developers disagree about whether the noImplicitAny flag should be true or false.
There is no correct answer and you can change the flag later.
But your choice now can make a difference in larger projects, so it merits discussion.
When the noImplicitAny flag is false (the default), and if
the compiler cannot infer the variable type based on how it's used,
the compiler silently defaults the type to any. That's what is meant by implicit any.
本文档在环境搭建时将noImplicitAny标志设置为true。
The documentation setup sets the noImplicitAny flag to true.
When the noImplicitAny flag is true and the TypeScript compiler cannot infer
the type, it still generates the JavaScript files, but it also reports an error.
Many seasoned developers prefer this stricter setting because type checking catches more
unintentional errors at compile time.
即使noImplicitAny标志被设置成了true,你也可以把变量的类型设置为any。
You can set a variable's type to any even when the noImplicitAny flag is true.
When the noImplicitAny flag is true, you may get implicit index errors as well.
Most developers feel that this particular error is more annoying than helpful.
You can suppress them with the following additional flag:
"suppressImplicitAnyIndexErrors":true
本文档在环境搭建时将noImplicitAny标志设置为true。
The documentation setup sets this flag to true as well.
Many JavaScript libraries, such as jQuery, the Jasmine testing library, and Angular,
extend the JavaScript environment with features and syntax
that the TypeScript compiler doesn't recognize natively.
When the compiler doesn't recognize something, it throws an error.
Many libraries include definition files in their npm packages where both the TypeScript compiler and editors
can find them. Angular is one such library.
The node_modules/@angular/core/ folder of any Angular application contains several d.ts files that describe parts of Angular.
我们不需要为那些包含了d.ts文件的库获取类型定义文件 —— Angular的所有包都是如此。
You need do nothing to get typings files for library packages that include d.ts files. Angular packages include them already.
TypeScript includes a special declaration file called lib.d.ts. This file contains the ambient declarations for various common JavaScript constructs present in JavaScript runtimes and the DOM.
Many libraries—jQuery, Jasmine, and Lodash among them—do not include d.ts files in their npm packages.
Fortunately, either their authors or community contributors have created separate d.ts files for these libraries and
published them in well-known locations.