From 3b297cd87cdd5e4806c13388abef78804f9af550 Mon Sep 17 00:00:00 2001 From: Mine Starks Date: Tue, 29 Aug 2017 17:16:51 -0700 Subject: [PATCH 1/2] advise against `T | any` in Common Mistakes --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e4c027648f..2165138bd6 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,9 @@ For a good example package, see [base64-js](https://github.com/DefinitelyTyped/D Example where it is not acceptable: `function parseJson(json: string): T;`. Exception: `new Map()` is OK. * Using the types `Function` and `Object` is almost never a good idea. In 99% of cases it's possible to specify a more specific type. Examples are `(x: number) => number` for [functions](http://www.typescriptlang.org/docs/handbook/functions.html#function-types) and `{ x: number, y: number }` for objects. If there is no certainty at all about the type, [`any`](http://www.typescriptlang.org/docs/handbook/basic-types.html#any) is the right choice, not `Object`. If the only known fact about the type is that it's some object, use the type [`object`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#object-type), not `Object` or `{ [key: string]: any }`. +* `var foo: string | any`: + When `any` is used in a union type, the resulting type is still `any`. So while the `string` portion of this type annotation may _look_ useful, it in fact offers no additional typechecking over simply using `any`. + Depending on the intention, acceptable alternatives could be: `any`, `string`, or `string | object`. #### Removing a package From 810a6aef295260b704f7396e5a70f9988b7b2521 Mon Sep 17 00:00:00 2001 From: Mine Starks Date: Wed, 30 Aug 2017 10:35:46 -0700 Subject: [PATCH 2/2] remove colon --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2165138bd6..5a5f39346d 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ For a good example package, see [base64-js](https://github.com/DefinitelyTyped/D * Using the types `Function` and `Object` is almost never a good idea. In 99% of cases it's possible to specify a more specific type. Examples are `(x: number) => number` for [functions](http://www.typescriptlang.org/docs/handbook/functions.html#function-types) and `{ x: number, y: number }` for objects. If there is no certainty at all about the type, [`any`](http://www.typescriptlang.org/docs/handbook/basic-types.html#any) is the right choice, not `Object`. If the only known fact about the type is that it's some object, use the type [`object`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#object-type), not `Object` or `{ [key: string]: any }`. * `var foo: string | any`: When `any` is used in a union type, the resulting type is still `any`. So while the `string` portion of this type annotation may _look_ useful, it in fact offers no additional typechecking over simply using `any`. - Depending on the intention, acceptable alternatives could be: `any`, `string`, or `string | object`. + Depending on the intention, acceptable alternatives could be `any`, `string`, or `string | object`. #### Removing a package