原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this article, we’ll dive into a set of tips and tricks that should come in handy in every Angular project and beyond when dealing with TypeScript.

In recent years, the need for static typing in JavaScript has increased rapidly. Growing front-end projects, more complex services, and elaborate command-line utilities have boosted the need for more defensive programming in the JavaScript world. Furthermore, the burden of compiling an application before actually running it hasn’t seen as a weakness, but rather as an opportunity. While two strong parties (TypeScript and Flow) have emerged, a lot of trends actually indicate that only one may prevail — TypeScript.

Besides the marketing claims and commonly known properties, TypeScript has an amazing community with very active contributors. It also has one of the best teams in terms of language design behind it. Led by Anders Hejlsberg, the team has managed to fully transform the landscape of large-scale JavaScript projects to be nearly an exclusively TypeScript-powered business. With very successful projects such as VSTS or Visual Studio Code, Microsoft themselves is a strong believer in this technology.

 
 
 
Ads by Hooly
 

But it’s not only the features of TypeScript that make the language appealing, but also the possibilities and frameworks that TypeScript is powering. Google’s decision to fully embrace TypeScript as their language of choice for Angular 2+ has proven to be a win-win. Not only did TypeScript gain more attention, but also Angular itself. Using static typing, the compiler can already give us informative warnings and useful explanations of why our code will not work.

TypeScript Tip 1: Supply Your Own Module Definitions

TypeScript is a superset of JavaScript. As such, every existing npm package can be utilized. While the TypeScript eco-system is huge, not all libraries are yet delivered with appropriate typings. Even worse, for some (smaller) packages not even separate declarations (in the form of @types/{package}) exist. At this point, we have two options:

  1. bring in legacy code using TypeScript tip 7
  2. define the API of the module ourselves.

The latter is definitely preferred. Not only do we have to look at the documentation of the module anyway, but typing it out will prevent simple mistakes during development. Furthermore, if we’re really satisfied with the typings that we just created, we can always submit them to @types for including them on npm. As such, this also rewards us with respect and gratefulness from the community. Nice!

What’s the easiest way to supply our own module definitions? Just create a module.d.ts in the source directory (or it could also be named like the package — for example, unknown-module.d.ts for an npm package unknown-module).

Let’s supply a sample definition for this module:

declare module 'unknown-module' {
const unknownModule: any;
export = unknownModule;
}

Obviously, this is just the first step, as we shouldn’t use any at all. (There are many reasons for this. TypeScript tip 5 shows how to avoid it.) However, it’s sufficient to teach TypeScript about the module and prevent compilation errors such as “unknown module ‘unknown-module’”. The export notation here is meant for the classic module.exports = ... kind of packages.

Here’s the potential consumption in TypeScript of such a module:

import * as unknownModule from 'unknown-module';

As already mentioned, the whole module definition is now placed in the type declaration of the exported constant. If the exported content is a function, the declaration could look like this:

declare module 'unknown-module' {
interface UnknownModuleFunction {
(): void;
}
const unknownModule: UnknownModuleFunction;
export = unknownModule;
}

Of course, it’s also possible to use packages that export functionality using the ES6 module syntax:

declare module 'unknown-module' {
interface UnknownModuleFunction {
(): void;
}
const unknownModule: UnknownModuleFunction;
export const constantA: number;
export const constantB: string;
export default unknownModule;
}

TypeScript Tip 2: Enum Vs Const Enum

TypeScript introduced the concept of enumerations to JavaScript, which did represent a collection of constants. The difference between

const Foo = {
A: 1,
B: 2,
};

and

enum Foo {
A = 1,
B = 2,
}

is not only of syntactical nature in TypeScript. While both will be compiled to an object (i.e., the first one will just stay as is, while the latter will be transformed by TypeScript), the TypeScript enum is protected and contains only constant members. As such, it would not be possible to define its values during runtime. Also, changes of these values will not be permitted by the TypeScript compiler.

This is also reflected in the signature. The latter has a constant signature, which is similar to

interface EnumFoo {
A: 1;
B: 2;
}

while the object is generalized:

interface ConstFoo {
A: number;
B: number;
}

Thus we wouldn’t see the values of these “constants” in our IDE. What does constenum now give us? First, let’s look at the syntax:

const enum Foo {
A = 1,
B = 2,
}

This is actually the same — but note, there’s a const in front. This little keyword makes a giant difference. Why? Because under these circumstances, TypeScript won’t compile anything. So we have the following cascade:

  • objects are untouched, but generate an implicit generalized shape declaration (interface)
  • enum will generate some boilerplate object-initializer along with a specialized shape declaration
  • const enum doesn’t generate anything beside a specialized shape declaration.

Now how is the latter then used in the code? By simple replacements. Consider this code:

enum Foo {
A = 1,
B = 2
} const enum Bar {
A = 1,
B = 2
} console.log(Bar.A, Foo.B);

Here we end up in JavaScript with the following outcome:

var Foo;
(function (Foo) {
Foo[Foo["A"] = 1] = "A";
Foo[Foo["B"] = 2] = "B";
})(Foo || (Foo = {}));
console.log(1 /* A */, Foo.B);

Note that 5 lines alone have been generated for enum Foo, while enum Bar only resulted in a simple replacement (constant injection). Thus const enum is a compile-time only feature, while the original enum is a runtime + compile-time feature. Most projects will be well suited for const enum, but there may be cases where enum is preferred.

TypeScript Tip 3: Type Expressions

Most of the time, we’re satisfied with using interface for defining new shapes of objects. However, there are cases when a simple interface isn’t sufficient any more. Consider the following example. We start with a simple interface:

interface StatusResponse {
issues: Array<string>;
status: 'healthy' | 'unhealthy';
}

The notation in 'healthy' | 'unhealthy' means either a constant string being healthy or another constant string equal to unhealthy. Alright, this is a sound interface definition. However, now we also have a method in our code, that wants to mutate an object of type StatusResponse:

function setHealthStatus(state: 'healthy' | 'unhealthy') {
// ...
}

So far, so good, but changing this now to 'healthy' | 'unhealthy' |'unknown' results in two changes already (one in the interface definition and one in the definition of argument type in the function). Not cool. Actually, the expressions we looked at until now are already type expressions, we just did not “store” them — that is, give them a name (sometimes called alias). Let’s do that:

type StatusResponseStatus = 'healthy' | 'unhealthy';

While constvar, and let create objects at runtime from JS expressions, typecreates a type declaration at compile-time from TS expressions (so-called type expressions). These type declarations can then be used:

interface StatusResponse {
issues: Array<string>;
status: StatusResponseStatus;
}

With such aliases in our tool belt, we can easily refactor the type system at will. Using TypeScript’s great type inference just propagates the changes accordingly.

TypeScript Tip 4: Use Discriminators

One of the uses of type expressions is the formerly introduced union of several (simple) type expressions — that is, type names or constants. Of course, the union is not restricted to simple type expressions, but for readability we should not come up with structures such as this:

type MyUnion = {
a: boolean,
b: number,
} | {
c: number,
d: {
sub: string,
}
} | {
(): void;
};

Instead, we want a simple and straightforward expression, such as this:

type MyUnion = TypeA | TypeB | TypeC;

Such a union can be used as a so-called discriminated union if all types expose at least one member with the same name, but a different (constant) value. Let’s suppose we have three types, such as these:

interface Line {
points: 2;
// other members, e.g., from, to, ...
} interface Triangle {
points: 3;
// other members, e.g., center, width, height
} interface Rectangle {
points: 4;
// other members, e.g., top, right, bottom, left
}

A discriminated union between these types could be this:

type Shape = Line | Triangle | Rectangle;

This new type can now be used in functions, where we can access specific members using some validation on the discriminator, which would be the points property. For example:

function calcArea(shape: Shape) {
switch (shape.points) {
case 2:
// ... incl. return
case 3:
// ... incl. return
case 4:
// ... incl. return
default:
return Math.NaN;
}
}

Naturally, switch statements come in quite handy for this task, but other means of validation can also be used.

Discriminated unions come in handy in all kinds of scenarios — for example, when traversing an AST-like structure of when dealing with JSON files that have a similar branching mechanism in their schema.

TypeScript Tip 5: Avoid Any Unless It Really Is Any

We’ve all been there: we know exactly what code to write, but we’re unable to satisfy the TypeScript compiler to accept our data model for the code. Well, luckily for us we can always fall back to any for saving the day. But we shouldn’t. any should only be used for types that can in fact be any. (For example, it’s on purpose that JSON.parsereturns any, as the outcome could be anything depending on the string we’re parsing.)

For instance, in one of our data stores we defined explicitly that a certain field custom will hold data of type any. We don’t know what will be set there, but the consumer is free to choose the data (and thus the data type). We neither wanted nor could prevent this from happening, so the type any was for real.

However, in most scenarios (that is, in all scenarios that are exclusively covered by our code) any is usually one or multiple types. We only need to find out what type exactly we expect and how to construct such a type to give TypeScript all the necessary information.

Using some of the previous tips — for example, TypeScript tip 4 and TypeScript tip 3 — we can already solve some of the biggest problems:

function squareValue(x: any) {
return Math.pow(x * 1, 2);
}

We’d much rather constrain the input as much as possible:

function squareValue(x: string | number) {
return Math.pow(+x, 2);
}

Now the interesting part is that the former expression x * 1 is allowed with any, but disallowed in general. However, the +x gives us the forced cast to a number as wanted. To check if our cast works with the given types, we need to be specific. The question “what types can enter here?” is a legit one that we need to answer before TypeScript can supply us with useful information.

TypeScript Tip 6: Use Generics Efficiently

TypeScript means static typing, but static typing doesn’t mean explicit typing. TypeScript has powerful type inference, which has to be used and fully understood before one can be really productive in TypeScript. Personally, I think I’ve become far more productive in TypeScript than plain JavaScript, as I don’t spend much time on my typings, yet everything seems to be in place and almost all trivial errors are already detected by TypeScript. One of the drivers behind this productivity boost is generics. Generics gives us the ability to bring in types as variables.

Let’s consider the following case of a classic JS helper function:

function getOrUpdateFromCache(key, cb) {
const value = getFromCache(key); if (value === undefined) {
const newValue = cb();
setInCache(key, newValue);
return newValue;
} return value;
}

Translating this directly to TypeScript leaves us behind with two anys: one is the data retrieved from the callback, and one from the function itself. However, this doesn’t need to look like that, since we obviously know the type (we pass in cb):

function getOrUpdateFromCache<T>(key: string, cb: () => T) {
const value: T = getFromCache(key); if (value === undefined) {
const newValue = cb();
setInCache(key, newValue);
return newValue;
} return value;
}

The only troublesome position in the code above is the explicit type assignment to the result of calling the getFromCache function. Here we must trust our code for the moment to consistently only use the same types for the same keys. In TypeScript tip 10 we learn how to improve this situation.

Most of the time the use of generics is just to “pass through” a type — that is, to teach TypeScript about the relation between certain argument types (in the former case the type of the result is connected to the return type of the callback). Teaching TypeScript about such relations can also be subject for further constraints, which are then put in place by TypeScript.

While generics is easy to use together with interfaces, types, classes, and standard functions, they may not seem so approachable with arrow functions. These functions are anonymous by definition (they need to be assigned to a variable to be accessed via a name).

As a rule of thumb, we can follow this approach: just think of a normal, but anonymous function declaration. Here only the name is gone. As such the <T> is naturally just placed before the parentheses. We end up with:

const getOrUpdateFromCache = <T>(key: string, cb: () => T) => /* ...*/;

However, once we would introduce this in a TSX file (for whatever reason), we would end up with an error ERROR : unclosed T tag. It’s the same problem that appears with casts (solved there by using the as operator). Now our workaround is to tell TypeScript explicitly that the syntax was intended for generics use:

const getOrUpdateFromCache = <T extends {}>(key: string, cb: () => T) => /* ...*/;

TypeScript Tip 7: Bring In Legacy Code

The key for migrating existing code to TypeScript has been a set of well-adjusted TypeScript configuration parameters — for example, to allow implicit any and to disable strict mode. The problem with this approach is that transformed code goes from a legacy state into a freeze state, which also impacts the new code that’s being written (since we disabled some of the most useful compiler options).

A better alternative is to just use allowJs in the tsconfig.json file, next to the usual (quite strong) parameters:

{
"compilerOptions": {
"allowJs": true,
// ...
}
}

Now instead of already renaming existing files from .js to .ts, we keep existing files as long as possible. We will only rename if we seriously can tackle the content in such a way that the code is fully transformed from JavaScript to a TypeScript variant that satisfies our settings.

TypeScript Tip 8: Create Functions With Properties

We already know that using interfaces to declare the shape of a function is a sound way. Furthermore, this approach allows us to attach some properties to the given function type. Let’s first see how this may look in practice:

interface PluginLoader {
(): void;
version: string;
}

Defining this is straightforward, but unfortunately, working with it isn’t. Let’s try to use this interface as intended by creating an object that fulfills the interface:

const pl: PluginLoader = () => {};
pl.version = '1.0.0';

Ouch: we can’t get past the declaration. TypeScript (correctly) complains, that the version property are missing. Okay, so how about the following workaround:

interface PluginLoaderLight {
(): void;
version?: string;
} const pl: PluginLoaderLight = () => {};
pl.version = '1.0.0';

Perfect. This works, but it has one major drawback: even though we know that past the pl.version assignment the version property will always exist at pl, TypeScript doesn’t know that. So from its point of view, any access to version could be wrong and needs to be checked against undefined first. In other words, in the current solution the interface we use for producing an object of this type has to be different from the interface used for consuming. This isn’t ideal.

Fortunately, there is a way around this problem. Let’s return to our original PluginLoader interface. Let’s try it with a cast that states to TypeScript “Trust me, I know what I’m doing”.

const pl = <PluginLoader>(() => {});
pl.version = '1.0.0';

The purpose of this is to tell TypeScript, “See this function, I know it will be of this given shape (PluginLoader)”. TypeScript still checks if this can be still fulfilled. Since there are no clashing definitions available, it will accept this cast. Casts should be our last line of defense. I don’t consider any a possible line of defense: either the type is any for real (can always be — we just accept anything, totally fine), or it should not be used and has to be replaced by something specific (see TypeScript tip 5).

While the way of casting may solve problems such as the described one, it may not be feasible in some non-Angular environment (for example, React components). Here, we need to choose the alternative variant of casting, namely the as operator:

const pl = (() => {}) as PluginLoader;
pl.version = '1.0.0';

Personally, I would always go for as-driven casts. Not only do they always work, they’re also quite readable even for someone who doesn’t have a TypeScript background. For me, consistency and readability are two principles that should always be at the core of every codebase. They can be broken, but there have to be good reasons for doing so.

TypeScript Tip 9: The Keyof Operator

TypeScript is actually quite good at — well — handling types. As such, it gives us some weapons that can be used to boilerplate some code for actually generating the content of an interface. Likewise, it also offers us options for iterating through the content of an interface.

Consider the following interface:

interface AbstractControllerMap {
user: UserControllerBase;
data: DataControllerBase;
settings: SettingsControllerBase;
//...
}

Potentially, in our code we have an object with a similar structure. The keys of this object are magic: its strings are used in many iterations and thus on many occasions. Quite likely we use these keys as arguments somewhere.

Obviously, we could just state that a function could look like this:

function actOnAbstractController(controllerName: string) {
// ...
}

The downside is that we definitely have more knowledge, which we do not share with TypeScript. A better version would therefore be this:

function actOnAbstractController(controllerName: 'user' | 'data' | 'settings') {
// ...
}

However, as already noted in TypeScript tip 3, we want to be resilient against refactorings. This is not resilient. If we add another key (that is, map another controller in our example above), we’ll need to edit the code in multiple locations.

A nice way out is provided by the keyof operator, which works against any type. For instance, aliasing the keys of the AbstractControllerMap above looks as follows:

type ControllerNames = keyof AbstractControllerMap;

Now we can change our function to truly become resilient against refactorings on the original map.

function actOnAbstractController(controllerName: ControllerNames) {
// ...
}

The cool thing about this is that keyof will actually respect interface merging. No matter where we place the keyof, it will always work against the “final” version of the type it’s applied to. This is also very useful when thinking about factory methods and efficient interface design for them.

TypeScript Tip 10: Efficient Callback Definitions

A problem that appears more often than anticipated is the typing of event handlers. Let’s look at the following interface for a second:

interface MyEventEmitter {
on(eventName: string, cb: (e: any) => void): void;
off(eventName: string, cb: (e: any) => void): void;
emit(eventName: string, event: any): void;
}

Looking back at all the previous tricks, we know that this design is neither ideal nor acceptable. So what can we do about it? Let’s start with a simple approximation to the problem. A first step is certainly to define all possible event names. We could use type expressions as introduced in TypeScript tip 3, but even better would be a mapping to the event type declarations like in the previous tip.

So we start with our map and apply TypeScript tip 9 to obtain the following:

interface AllEvents {
click: any;
hover: any;
// ...
} type AllEventNames = keyof AllEvents;

This has already some effect. The previous interface definition now becomes:

interface MyEventEmitter {
on(eventName: AllEventNames, cb: (e: any) => void): void;
off(eventName: AllEventNames, cb: (e: any) => void): void;
emit(eventName: AllEventNames, event: any): void;
}

A little bit better, but we still have any on all interesting positions. Now TypeScript tip 6 can be applied to make TypeScript a little bit more knowledgeable about the entered eventName:

interface MyEventEmitter {
on<T extends AllEventNames>(eventName: T, cb: (e: any) => void): void;
off<T extends AllEventNames>(eventName: T, cb: (e: any) => void): void;
emit<T extends AllEventNames>(eventName: T, event: any): void;
}

This is good, but not sufficient. TypeScript now knows about the exact type of eventName when we enter it, but we’re unable to use the information stored in T for anything. Except, we can use it with another powerful type expressions: index operators applied to interfaces.

interface MyEventEmitter {
on<T extends AllEventNames>(eventName: T, cb: (e: AllEvents[T]) => void): void;
off<T extends AllEventNames>(eventName: T, cb: (e: AllEvents[T]) => void): void;
emit<T extends AllEventNames>(eventName: T, event: AllEvents[T]): void;
}

This seems to be powerful stuff, except that our existing declarations are all set to any. So let’s change this.

interface ClickEvent {
leftButton: boolean;
rightButton: boolean;
} interface AllEvents {
click: ClickEvent;
// ...
}

The real powerful part is now that interface merging still works. That is, we can extend our event definitions out of place by using the same interface name again:

interface AllEvents {
custom: {
field: string;
};
}

This makes type expressions even more powerful, as the extensibility is integrated in a wonderful and elegant way.

Further Reading

Conclusion

Hopefully one or more of these TypeScript tips were new to you or at least something you wanted to see in a closer write up. The list is far from complete, but should give you a good starting point to avoid some problems and increase productivity.

What tricks make your code shine? Where do you feel most comfortable in? Let us know in the comments!

10 Essential TypeScript Tips And Tricks For Angular Devs的更多相关文章

  1. 10 Interesting Linux Command Line Tricks and Tips Worth Knowing

    I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...

  2. (转) How to Train a GAN? Tips and tricks to make GANs work

    How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...

  3. [转]Tips——Chrome DevTools - 25 Tips and Tricks

    Chrome DevTools - 25 Tips and Tricks 原文地址:https://www.keycdn.com/blog/chrome-devtools 如何打开? 1.从浏览器菜单 ...

  4. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips- ...

  5. Matlab tips and tricks

    matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...

  6. LoadRunner AJAX TruClient协议Tips and Tricks

    LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...

  7. Android Studio tips and tricks 翻译学习

    Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...

  8. Tips and Tricks for Debugging in chrome

    Tips and Tricks for Debugging in chrome Pretty print On sources panel ,clicking on the {} on the bot ...

  9. WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks

    What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...

随机推荐

  1. appium的使用

    这套教程年久失修,问题的人也比较多,于是,我重新整理了一套appium入门教程. appium新手入门(1)—— appium介绍 appium新手入门(2)—— 安装 Android SDK app ...

  2. PC机做ISCSI存储服务器故障

    物理主机:IBM x3650 6块SAS盘,分为两组RAID.一组系统,一组数据. zabbix监控告警情况如下: 早上上班,发现服务器无法连接,网络无法通信.让IDC重启,还是无法恢复正常. 去了机 ...

  3. 带着问题学git

    序 作为git新手,常见的git clone,push,commit命令已经足够完成一次代码的发布,但是如果不幸碰到问题往往会束手无策,利用网络问答解决了之后也不知其所以然.所以,做一次好奇宝宝吧! ...

  4. Java-事务管理

    1.事务的概念: 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. 2. 管理事务: 2.1. 数据库默认的事务 数据库默认支持事务的,但是数据库默认的事务是一条sql语 ...

  5. 配置虚拟主机 和 打war包

    配置一台虚拟主机?        在[tomcat]/conf/server.xml文件中的<Engine>标签内部添加一个<Host>标签:            <H ...

  6. hdu6162(树链剖分)

    hdu6162 题意 给出一颗带点权的树,每次询问一对节点 \((u, v)\),问 \(u\) 到 \(v\) 的最短路径上所有节点权值在 \([c1, c2]\) 区间内的和. 分析 树链剖分,那 ...

  7. [BZOJ4650][NOI2016]优秀的拆分(SAM构建SA)

    关于解法这个讲的很清楚了,主要用了设关键点的巧妙思想. 主要想说的是一个刚学的方法:通过后缀自动机建立后缀树,再转成后缀数组. 后缀数组功能强大,但是最令人头疼的地方是模板太难背容易写错.用这个方法, ...

  8. [LOJ#2540][PKUWC2018]随机算法(概率DP)

    场上数据很水,比较暴力的做法都可以过90分以上,下面说几个做法. 1. 暴力枚举所有最大独立集,对每个独立集分别DP.复杂度玄学,但是由于最大独立集并不多,所以可以拿90. 2. dp[S][k]表示 ...

  9. java web定时任务---quartz

    写在前面: 前面有简单的记录下Timer定时的用法,但是在此次项目中,选择的是quartz来完成定时操作任务的.两者都可以完成定时操作,但是spring可以整合quartz,并且配置起来也比较简便,还 ...

  10. Problem Y: 零起点学算法21——摄氏温度转换

    #include<stdio.h> int main() { float f,c; while(scanf("%f",&f)!=EOF) c=*(f-); pr ...