From 73ca2dfb779d29fd72d768d19ce9969b2f2315d9 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 1 Dec 2021 16:05:16 +0530 Subject: [PATCH 1/5] Add Record and fix typo --- doc/TS-MIGRATION.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md index d7ebc7f4..f3773747 100644 --- a/doc/TS-MIGRATION.md +++ b/doc/TS-MIGRATION.md @@ -2,8 +2,12 @@ ## Use `type` rather than `interface` for named parameters and POJO return values. -`type` and `interface` can be used somewhat interchangebly used, but let's use `type` to describe data and `interface` to describe (polymorphic) behaviour. +`type` and `interface` can be used somewhat interchangeably, but let's use `type` to describe data and `interface` to describe (polymorphic) behaviour. Good examples of data are option objects to have named parameters, and POJO (plain old javascript objects) without any methods, just fields. Also see [this playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBACghgJwgO2AeTMAlge2QZygF4oBvAKCiqmTgFsIAuKfYBLZAcwG5LqATCABs4IAPzNkAVzoAjCAl4BfcuVCQoAYQAWWIfwzY8hEvCSpDuAlABkZPlQDGOITgTNW7LstWOR+QjMUYHtqKGcCNilHYDcAChxMK3xmIIsk4wBKewcoFRVyPzgArV19KAgAD2AUfkDEYNDqCM9o2IQEjIJmHT0DLvxsijCw-ClIDsSjAkzeEebjEIYAuE5oEgADABJSKeSAOloGJSgsQh29433nVwQlDbnqfKA) + +## Use `Record` to describe a type that accepts any Javascript object. + +Prefer this over index signature, `any` or `object`. From 928a5c27f3b1391153c5a1b670dec67cd243ac4c Mon Sep 17 00:00:00 2001 From: R Midhun Suresh Date: Wed, 1 Dec 2021 22:50:59 +0530 Subject: [PATCH 2/5] Add rationale Co-authored-by: Bruno Windels --- doc/TS-MIGRATION.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md index f3773747..82e88a89 100644 --- a/doc/TS-MIGRATION.md +++ b/doc/TS-MIGRATION.md @@ -10,4 +10,4 @@ Also see [this playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAC ## Use `Record` to describe a type that accepts any Javascript object. -Prefer this over index signature, `any` or `object`. +Record allows us to avoid passing in primitive types and prevents type errors when accessing properties. As this is a temporary type while converting javascript, it seems best to not add any additional requirements. If any errors occur, they must have already been present, and we should fix it by adding proper types. So prefer `Record` over `[key: string]: any`, `any` or `object`. From ef3456199c4bea90695ec1fcbcd8ce3503207735 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 1 Dec 2021 22:52:09 +0530 Subject: [PATCH 3/5] Fix formatting --- doc/TS-MIGRATION.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md index 82e88a89..a7342ba8 100644 --- a/doc/TS-MIGRATION.md +++ b/doc/TS-MIGRATION.md @@ -10,4 +10,4 @@ Also see [this playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAC ## Use `Record` to describe a type that accepts any Javascript object. -Record allows us to avoid passing in primitive types and prevents type errors when accessing properties. As this is a temporary type while converting javascript, it seems best to not add any additional requirements. If any errors occur, they must have already been present, and we should fix it by adding proper types. So prefer `Record` over `[key: string]: any`, `any` or `object`. +`Record` allows us to avoid passing in primitive types and prevents type errors when accessing properties. As this is a temporary type while converting javascript, it seems best to not add any additional requirements. If any errors occur, they must have already been present, and we should fix it by adding proper types. So prefer `Record` over `[key: string]: any`, `any` or `object`. From 5ef7ab32dfc2a6bcd9ad810e4a3ec3a3961bcf7e Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 10 Dec 2021 12:09:18 +0530 Subject: [PATCH 4/5] Update doc --- doc/TS-MIGRATION.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md index a7342ba8..78a4c9ba 100644 --- a/doc/TS-MIGRATION.md +++ b/doc/TS-MIGRATION.md @@ -8,6 +8,30 @@ Good examples of data are option objects to have named parameters, and POJO (pla Also see [this playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBACghgJwgO2AeTMAlge2QZygF4oBvAKCiqmTgFsIAuKfYBLZAcwG5LqATCABs4IAPzNkAVzoAjCAl4BfcuVCQoAYQAWWIfwzY8hEvCSpDuAlABkZPlQDGOITgTNW7LstWOR+QjMUYHtqKGcCNilHYDcAChxMK3xmIIsk4wBKewcoFRVyPzgArV19KAgAD2AUfkDEYNDqCM9o2IQEjIJmHT0DLvxsijCw-ClIDsSjAkzeEebjEIYAuE5oEgADABJSKeSAOloGJSgsQh29433nVwQlDbnqfKA) -## Use `Record` to describe a type that accepts any Javascript object. +## Use `type foo = { [key: string]: any }` for types that you intend to fill in later. -`Record` allows us to avoid passing in primitive types and prevents type errors when accessing properties. As this is a temporary type while converting javascript, it seems best to not add any additional requirements. If any errors occur, they must have already been present, and we should fix it by adding proper types. So prefer `Record` over `[key: string]: any`, `any` or `object`. +For instance, if you have a method such as: +```js + function load(options) { + // ... + } +``` +and you intend to type options at some later point, do: +```ts + type Options = { [key: string]: any} +``` +This makes it much easier to add the necessary type information at a later time. + +## Use `object` or `Record` to describe a type that accepts any javascript object. + +Sometimes a function or method may genuinely need to accept any object; eg: +```js +function encodeBody(body) { + // ... +} +``` +In this scenario: +- Use `object` if you know that you will not access any property +- Use `Record` if you need to access some property + +Both usages prevent the type from accepting primitives (eg: string, boolean...). If using `Record`, ensure you have guards to check that the properties really do exist. From 10368500f2a2b4b56421659a46dc3473d7b2094c Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 10 Dec 2021 12:12:52 +0530 Subject: [PATCH 5/5] Fix formatting --- doc/TS-MIGRATION.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/TS-MIGRATION.md b/doc/TS-MIGRATION.md index 78a4c9ba..b63966f2 100644 --- a/doc/TS-MIGRATION.md +++ b/doc/TS-MIGRATION.md @@ -34,4 +34,5 @@ In this scenario: - Use `object` if you know that you will not access any property - Use `Record` if you need to access some property -Both usages prevent the type from accepting primitives (eg: string, boolean...). If using `Record`, ensure you have guards to check that the properties really do exist. +Both usages prevent the type from accepting primitives (eg: string, boolean...). +If using `Record`, ensure that you have guards to check that the properties really do exist.