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

Unsafely select keys from an object #31

Open
eckertalex opened this issue Jun 2, 2022 · 2 comments
Open

Unsafely select keys from an object #31

eckertalex opened this issue Jun 2, 2022 · 2 comments
Assignees
Labels

Comments

@eckertalex
Copy link

How can I select just a subset of keys from an object?

Consider this example in an express app. I want to validate the body, params, and query and want to use ts-belt to select just those three from the request. Is there a way to do this with ts-belt? Maybe a unsafeSelectKeys?

const validKeys = ['body', 'params', 'query']

const validReq = D.selectKeys(request, validKeys)

Thanks for the help!

@mobily
Copy link
Owner

mobily commented Jun 5, 2022

@eckertalex sounds like a great idea, do you have any suggestion on how to tackle TS signature of D.unsafeSelectKeys?

@eckertalex
Copy link
Author

I have been thinking about it the last couple of days and I am not sure if it is possible to have the correct typings if the keys exist and unknown if it.

In pseudo Typescript maybe something like this?

function selectKeysUnsafe<T, K extends Either<keyof T, unknown>>(dict: T, keys: ReadonlyArray<K>): Pick<T, K>

Basically the type of the keys would be either a key of T or unknown. But I think that way we would not have the correct return type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants