[Basic] Example of indexed access types

4
closed
mayank99
mayank99
Posted 5 months ago

[Basic] Example of indexed access types #434

What cheatsheet is this about? Basic cheatsheet

What's your idea?

I recently came across the need for accessing a specific property from a type (indexed access types).

type Props = {
  // ...
  status?: 'available' | 'away' | 'busy';
};

type CustomStatus = Props['status'] | 'offline';
                    ^^^^^^^^^^^^^^^

It took me a lot of searching and playing around to learn that this was even possible. And this cheatsheet (which is normally very helpful, so thanks for that!) is the first place I looked😅 It would be nice if this was part of the cheatsheet.

Reproduction in TypeScript Playground

Additional info

My biggest source of confusion was that it won't allow you to do something like:

type CustomStatus = Props.status;

or

import { Props.status } from './Props';
sw-yx
sw-yx
Created 5 months ago

thanks! any suggestion of where you'd expect to find this info?

mayank99
mayank99
Created 5 months ago

Maybe under The Types I need don't exist!? As long as it's searchable, I think it should be ok

sw-yx
sw-yx
Created 5 months ago

ok :) contributions welcome otherwise i wont get to this right away myself

stale[bot]
stale[bot]
Created 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!