From 15740366bb21bc144aeb2a2dd1b03a99308f41c2 Mon Sep 17 00:00:00 2001 From: Asaf S Date: Wed, 9 Nov 2022 05:21:24 +0200 Subject: [PATCH 1/2] Update Indexed Access Types.md The documentation wrongfully states that a constant cannot be used to index, but that's not true, as this is caused by a misfitting type that could be solved by using 'as const'. --- .../Type Manipulation/Indexed Access Types.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md b/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md index b954513e8a03..19f600dd04e2 100644 --- a/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md +++ b/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md @@ -57,17 +57,21 @@ type Age2 = Person["age"]; // ^? ``` -You can only use types when indexing, meaning you can't use a `const` to make a variable reference: +You can only use types when indexing, so pay attention variable's type of the index. +For example, when using `const` to make a variable reference, pay attention that its type is `string` and not `"age"` (this can be changed by using [`as const`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions): ```ts twoslash // @errors: 2538 2749 type Person = { age: number; name: string; alive: boolean }; // ---cut--- -const key = "age"; -type Age = Person[key]; +const key = "age"; // Type is 'string' +type Age1 = Person[key]; + +const keyWithConstantType = "age" as const; // Type is "age" +type Age2 = Person[keyWithConstantType]; ``` -However, you can use a type alias for a similar style of refactor: +Moreover, you can use a type alias for a similar style of refactor: ```ts twoslash type Person = { age: number; name: string; alive: boolean }; From 1e89ce26cbe8709dc575623431f1c8690b715700 Mon Sep 17 00:00:00 2001 From: Asaf S Date: Sun, 4 Jun 2023 14:53:38 +0300 Subject: [PATCH 2/2] Update Indexed Access Types.md --- .../handbook-v2/Type Manipulation/Indexed Access Types.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md b/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md index 19f600dd04e2..7a669debfeb0 100644 --- a/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md +++ b/packages/documentation/copy/en/handbook-v2/Type Manipulation/Indexed Access Types.md @@ -57,17 +57,17 @@ type Age2 = Person["age"]; // ^? ``` -You can only use types when indexing, so pay attention variable's type of the index. +When indexing, you cannot use types that are wider than the keys of the object being indexed, so pay attention of the variable's type used to index. For example, when using `const` to make a variable reference, pay attention that its type is `string` and not `"age"` (this can be changed by using [`as const`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions): ```ts twoslash // @errors: 2538 2749 type Person = { age: number; name: string; alive: boolean }; // ---cut--- -const key = "age"; // Type is 'string' +const key = "age"; // The type is 'string', which is too wide (e.g. "foo" is also a string, but not one of Person's keys), so the next line won't compile type Age1 = Person[key]; -const keyWithConstantType = "age" as const; // Type is "age" +const keyWithConstantType = "age" as const; // Type is "age", which is a key in Person, so that will work type Age2 = Person[keyWithConstantType]; ```