Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Indexed Access Types.md #2603

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open

Update Indexed Access Types.md #2603

wants to merge 2 commits into from

Conversation

Asaf-S
Copy link

@Asaf-S Asaf-S commented Nov 9, 2022

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'.

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'.
Copy link
Contributor

@orta orta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement!

@Asaf-S
Copy link
Author

Asaf-S commented Nov 13, 2022

Is something wrong with the tests?

Comment on lines 67 to 71
const key = "age"; // Type is 'string'
type Age1 = Person[key];

const keyWithConstantType = "age" as const; // Type is "age"
type Age2 = Person[keyWithConstantType];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused by this; aren't both invalid? Is that what you're trying to show here? Isn't the problem not to do with const, but that you can't index a type with something in value space, and therefore need to typeof something or use a type?

I would think that if anything, the old stuff could stay, but instead say something like:

You can only use types when indexing. For example, you cannot index a type with a variable:

And maybe include a typeof?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Example 1
type Age1 = Person["age"]; // Success

// Example 2
const key2 = "age";
type Age2 = Person[key]; // Failure

// Example 3
const key3 = "age" as const;
type Age3 = Person[key]; // Success

In example 1 above, "age" can be used to index Person as it's one of Person's keys.

In example 2 above, the reason why key2 fails - is because the TypeScript type of key is string, and it's too wide for a key of the Person type.
For example: bla is a valid string but it's not a key of Person, and therefore key2 is not fit to index Person.
For that reason, the existing documentation says users should use a constant as an index.

In example 3 above, the reason why key3 succeeds - is that the TypeScript type of key is "age" (and not string), which is a key of the Person type.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've edited my PR to be more self-explaining

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I apparently missed the push.

What I'm saying is that it isn't behaving the way you're describing; your third example fails to compile: Playground Link

Copy link
Member

@rbuckton rbuckton Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue isn't as const, the issue is you need to use typeof key2 and typeof key3: Playground link

While Computed Property Names in Object Type Literals and Interface Types use values, Indexed Access Types can only use types:

interface List<T> {
  [Symbol.iterator](): Iterator<T>;
// ^^^^^^^^^^^^^^^-- 'Symbol.iterator' here used as a value
}

type ListIterator = List<number>[typeof Symbol.iterator];
//                               ^^^^^^-- must use 'typeof' here to read its type

@rbuckton
Copy link
Member

I think the original statement is mostly accurate ("you can't use a const to make a variable reference"), what's actually missing is a qualifying statement and additional example related to typeof, i.e.

However, you can use a type alias for a similar style of refactor:

...

Or, you can perform a type query using typeof to get the type of the const variable:

const key = "age";
type Age = Person[typeof key];

@jakebailey jakebailey closed this Jun 5, 2024
@jakebailey jakebailey reopened this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants