Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract keys of type (key type, not its field type) #35

Closed
regevbr opened this issue Aug 18, 2019 · 6 comments
Closed

extract keys of type (key type, not its field type) #35

regevbr opened this issue Aug 18, 2019 · 6 comments
Assignees
Labels
question More info is requested wiki It's nice to learn stuff

Comments

@regevbr
Copy link
Contributor

regevbr commented Aug 18, 2019

馃崺 Feature Request

Describe the solution you'd like

Since keys of an object can be of type string|number|symbol and sometimes types require only string values, it will be handy to have a type that extracts only the string key (or of any type for that matter)

type Index = string | number | symbol; // I think we should make that type public!

type KeysOfType<T extends object, M extends Index> = Extract<keyof T, M>;

type t = KeysOfType<{
  a: number;
  2: number;
}, string>; // "a"

@pirix-gh thoughts?

@regevbr regevbr changed the title extract keys of type (key type not its field type) extract keys of type (key type, not its field type) Aug 18, 2019
@millsp
Copy link
Owner

millsp commented Aug 18, 2019

@regevbr , I currently achieve this by doing [keys] & [type]. For example:

('1' | 1) & string // '1'

I rather raise awareness of such techniques instead of writing such types.

So I am marking this as a question :)

@regevbr
Copy link
Contributor Author

regevbr commented Aug 18, 2019

That makes sense, although less intuitive in my view. Thought this type will make it more verbose and easier to discover in your library

@millsp millsp self-assigned this Aug 18, 2019
@millsp millsp added question More info is requested wiki It's nice to learn stuff labels Aug 18, 2019
@millsp
Copy link
Owner

millsp commented Aug 18, 2019

You can still be verbose (but more generic) with https://pirix-gh.github.io/ts-toolbelt/modules/_union_filter_.html#filter

@millsp
Copy link
Owner

millsp commented Aug 18, 2019

@millsp millsp closed this as completed Aug 18, 2019
@regevbr
Copy link
Contributor Author

regevbr commented Aug 18, 2019

@pirix-gh do you think we can at least expose

type Index = string | number | symbol;

in the public interface?

@millsp
Copy link
Owner

millsp commented Aug 18, 2019

@regevbr Yes, I also found myself needing it a few times

Repository owner locked and limited conversation to collaborators Feb 2, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question More info is requested wiki It's nice to learn stuff
Projects
None yet
Development

No branches or pull requests

2 participants