Dieser Artikel ist auch auf Deutsch verfügbar
let, with which the scope of variables can be handled more simply.
The interface defined here provides methods for manipulating the shape (
move), rendering on a
<canvas> element (
render), and calculating the object size (
size). The latter returns a pair of two numbers, which can be used as follows:
Although the interface declaration looks exactly the same as in many other object-oriented languages, TypeScript takes a unique approach: When type checking, it doesn’t matter which interface a method comes from – TypeScript only checks the method signature. Here is an example of this: The interface above is seen as compatible with this one, though it doesn’t extend it.
Incidentally, the compiler would complain about this (incomplete) definition because some of the methods are not implemented. Just like in Java, multiple inheritance of interfaces is allowed. However, there may only be a maximum of one superclass.
any type, which effectively eliminates further checking. Practically, developers are back to square one and have gained nothing. In order to solve this problem, the development community brought to life the “DefinitelyTyped” repository. At the time of writing, 7,000 packages with type definition were available there. Using them couldn’t be easier. For React, for example, the following call is sufficient:
This allows developers to download both the React library and its corresponding types. The TypeScript compiler then automatically finds them and includes them in the checking process. These packages are files in
.d.ts format, i.e., TypeScript source code without implementation of methods, consisting only of pure type declarations. A whole slew of other libraries deliver these
.d.ts files directly, without the detour via DefinitelyTyped. For most packages, the type definitions are available in one of these two ways. In practice, developers can always rely on the type checks by the compiler.
await. TypeScript had supported it since 2015 already. Used carefully, developers can take advantage of this head start to develop new features in their own code faster and more reliably. This gives them a real competitive advantage.
Also in current compiler versions developers can configure the so-called target and in this way select an ES version as far back as ES3 (from the year 1999).
Development with TypeScript
TypeScript can already show off its advantages in the simple development of applications with Vanilla JS – i.e., when developers build a website via server-side rendering and without frontend frameworks. Modern browsers come with numerous powerful APIs that cover almost all needs. Paired with an editor like VS Code, TypeScript can provide good tooling support for this, for example with code completion.
An almost life-saving feature is that TypeScript also recognizes certain “magic strings” and “magic constants.” For example, if developers want to query a resource at runtime via
fetch, they can pass numerous configuration options via string. Thanks to the type checker, they no longer need to remember whether it is
mode: “same-origin” or
mode: “sameorigin”. (The former is correct, the latter is underlined in red in the editor.)