Skip to content

ValidPath without const assertion? #119

Closed

Description

@asgerhallas

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

Activity

  1. millsp commented on Jun 17, 2020

    @millsp
    Owner

    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.

  2. added
    question
    on Jul 9, 2020
  3. asgerhallas commented on Nov 27, 2020

    @asgerhallas
    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.

  4. millsp commented on Nov 27, 2020

    @millsp
    Owner

    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:

  5. asgerhallas commented on Nov 27, 2020

    @asgerhallas
    Author

    Thanks a lot for your swift reply :)

    I'll look into those links right away!

  6. millsp commented on Jan 4, 2021

    @millsp
    Owner

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

  7. asgerhallas commented on Jan 7, 2021

    @asgerhallas
    Author

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

  8. Repository owner locked and limited conversation to collaborators on Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions