You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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:
Activity
millsp commentedon Jun 17, 2020
Hi there,
Without a const assertion, the type of
['case', 'name']would be narrowed tostring[]that's all. Depending on your type safety needs, you would chooseas constor not.asgerhallas commentedon Nov 27, 2020
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 forgetas constand thus we will not catch errors now or when the allowed string values change in the future.millsp commentedon 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 commentedon Nov 27, 2020
Thanks a lot for your swift reply :)
I'll look into those links right away!
millsp commentedon Jan 4, 2021
I posted a solution to this problem here in microsoft/TypeScript#30680. It will land soon in ts-toolbelt.
asgerhallas commentedon Jan 7, 2021
This is amazing, thank you for doing this! I look forward to it landing is ts-toolbelt too.