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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidPath without const assertion? #119

Closed
asgerhallas opened this issue Jun 16, 2020 · 6 comments
Closed

ValidPath without const assertion? #119

asgerhallas opened this issue Jun 16, 2020 · 6 comments
Labels
question More info is requested

Comments

@asgerhallas
Copy link

🤔 Question

I'm new to this library, and am still trying to get my head around it. It's very impressive! :)

In this sample https://pirix-gh.github.io/ts-toolbelt/modules/_object_pathvalid_.html there's the following code:

const test0 = getAt(state,          ['case', 'name'] as const) // {c: number}

Why is const needed? And is there a way to make it work without the const assertion?

Hope you can help.

@millsp
Copy link
Owner

millsp commented Jun 17, 2020

Hi there,

Without a const assertion, the type of ['case', 'name'] would be narrowed to string[] that's all. Depending on your type safety needs, you would choose as const or not.

@millsp millsp closed this as completed Jun 17, 2020
@millsp millsp added the question More info is requested label Jul 9, 2020
@asgerhallas
Copy link
Author

I somehow missed your answer, sorry! What I meant was that, I'd like the type safety to be enforced by the function signature, so the caller does not have to add as const, but still only be able to pass expected strings in the array. It's easy for a caller to forget as const and thus we will not catch errors now or when the allowed string values change in the future.

@millsp
Copy link
Owner

millsp commented Nov 27, 2020

It's a TypeScript limitation, I know how inconvenient this is. To work around that, the only solution is to take rest parameters instead. Or you could have this Maybe this can help you:

@asgerhallas
Copy link
Author

Thanks a lot for your swift reply :)

I'll look into those links right away!

@millsp
Copy link
Owner

millsp commented Jan 4, 2021

I posted a solution to this problem here in microsoft/TypeScript#30680. It will land soon in ts-toolbelt.

@asgerhallas
Copy link
Author

asgerhallas commented Jan 7, 2021

This is amazing, thank you for doing this! I look forward to it landing is ts-toolbelt too.

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
Projects
None yet
Development

No branches or pull requests

2 participants